编辑文章 SpringBootSpringBoot ## 1添加pom ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> ``` ## 2编写配置类 ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //swagger要扫描的包路径 .apis(RequestHandlerSelectors.basePackage("com.learn.springboot.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springboot Swagger 测试") .description("Springboot 整合Swagger2") .termsOfServiceUrl("localhost:8080/") .contact(new Contact("Swagger测试","liflag.cn","229670104@qq.com")) .version("1.0") .build(); } } ``` ## 3 添加注解 ``` @Api(value = "测试接口") @Controller public class HelloController { @Autowired UserMapper userMapper; @ApiOperation(value = "测试借口", notes = "访问hello") @ApiImplicitParam(name = "id", value = "用户id", required=true, dataType = "String") @RequestMapping("/hello") @ResponseBody public String hello(String id) { User user = userMapper.selectByPrimaryKey(Integer.valueOf(id)); System.out.println(user.getName()); return "helloWorld "+ user.getName(); } } ``` 4 访问接口文档  # swagger说明  ## 常用注解 - @Api()用于类; 表示标识这个类是swagger的资源 - @ApiOperation()用于方法; 表示一个http请求的操作 - @ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等) - @ApiModel()用于类 表示对类进行说明,用于参数用实体类接收 - @ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改 - @ApiIgnore()用于类,方法,方法参数 表示这个方法或者类被忽略 - @ApiImplicitParam() 用于方法 表示单独的请求参数 - @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam 例子[https://www.cnblogs.com/mao2080/p/9021714.html](https://www.cnblogs.com/mao2080/p/9021714.html) <h2>1添加pom</h2> <pre><code><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency> </code></pre> <h2>2编写配置类</h2> <pre><code>@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //swagger要扫描的包路径 .apis(RequestHandlerSelectors.basePackage("com.learn.springboot.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springboot Swagger 测试") .description("Springboot 整合Swagger2") .termsOfServiceUrl("localhost:8080/") .contact(new Contact("Swagger测试","liflag.cn","229670104@qq.com")) .version("1.0") .build(); } } </code></pre> <h2>3 添加注解</h2> <pre><code>@Api(value = "测试接口") @Controller public class HelloController { @Autowired UserMapper userMapper; @ApiOperation(value = "测试借口", notes = "访问hello") @ApiImplicitParam(name = "id", value = "用户id", required=true, dataType = "String") @RequestMapping("/hello") @ResponseBody public String hello(String id) { User user = userMapper.selectByPrimaryKey(Integer.valueOf(id)); System.out.println(user.getName()); return "helloWorld "+ user.getName(); } } </code></pre> <p>4 访问接口文档 <img src="http://img.dafeiyu.cn/file/2019/6/db364e63a67b449196626183c292aee6-image.png" alt="db364e63a67b449196626183c292aee6-image.png" /></p> <h1>swagger说明</h1> <p><img src="http://img.dafeiyu.cn/file/2019/6/105b3131d12c4865b303b0592ae97282-image.png" alt="105b3131d12c4865b303b0592ae97282-image.png" /></p> <h2>常用注解</h2> <ul> <li>@Api()用于类; 表示标识这个类是swagger的资源</li> <li>@ApiOperation()用于方法; 表示一个http请求的操作</li> <li>@ApiParam()用于方法,参数,字段说明; 表示对参数的添加元数据(说明或是否必填等)</li> <li>@ApiModel()用于类 表示对类进行说明,用于参数用实体类接收</li> <li>@ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改</li> <li>@ApiIgnore()用于类,方法,方法参数 表示这个方法或者类被忽略</li> <li>@ApiImplicitParam() 用于方法 表示单独的请求参数</li> <li>@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam</li> </ul> <p>例子<a href="https://www.cnblogs.com/mao2080/p/9021714.html">https://www.cnblogs.com/mao2080/p/9021714.html</a></p> 标题: 分类: 请选择分类(必选) 博客 酷玩 分享 相册 标签: 按 Tab、回车或逗号把输入的文字变成标签;点 × 或在输入框为空时按退格删除;最多 8 个 封面图: 上传图片 建议尺寸 800x400,图片大小不超过 1MB 展开更多设置(关键词、摘要、转载、评论) 关键词: 摘要: 1添加pom <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId&g... 选项: 是否转载 启用评论 勾选「是否转载」可标注文章来源,原文链接可留空。 正文: 保存修改