springcloud引入swagger(转)

一、引入依赖:


 
 
  1. <dependency>
  2. <groupId>io.springfox </groupId>
  3. <artifactId>springfox-swagger2 </artifactId>
  4. <version>2.5.0 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox </groupId>
  8. <artifactId>springfox-swagger-ui </artifactId>
  9. <version>2.5.0 </version>
  10. </dependency>

注意了注意了知识点! 

版本一定要2.5.0以及以上版本! 网上博文大多数引的2.2.2版本的, 这个版本在demo中没有问题, 但是开发中你肯定会引别的插件.

2.2.2版本的与feign有冲突! 会报bean创建加载异常!  这是个坑不想网友们再遇到同样的错误.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

二、Swagger2配置文件类:


 
 
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import springfox.documentation.builders.ApiInfoBuilder;
  4. import springfox.documentation.builders.PathSelectors;
  5. import springfox.documentation.builders.RequestHandlerSelectors;
  6. import springfox.documentation.service.ApiInfo;
  7. import springfox.documentation.spi.DocumentationType;
  8. import springfox.documentation.spring.web.plugins.Docket;
  9. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  10. /**
  11. * @ClassName: swagger2配置
  12. * @Description: TODO
  13. * @author 刘圈圈
  14. * @date 2018年7月5日
  15. */
  16. @Configuration
  17. @EnableSwagger2
  18. public class Swagger2 {
  19. @Bean
  20. public Docket createRestApi() {
  21. return new Docket(DocumentationType.SWAGGER_2)
  22. .apiInfo(apiInfo())
  23. .select()
  24. .apis(RequestHandlerSelectors.basePackage( "io.sr.modules.tra.app"))
  25. .paths(PathSelectors.any())
  26. .build();
  27. }
  28. private ApiInfo apiInfo() {
  29. return new ApiInfoBuilder()
  30. .title( "旅游用车 APIs")
  31. .description( "--------------------------------")
  32. .termsOfServiceUrl( "https://blog.csdn.net/ityqing")
  33. .contact( "刘圈圈")
  34. .version( "0.1.1")
  35. .build();
  36. }
  37. }

这个类要和启动类放在同一个目录下, 

用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。

Application.class 加上注解@EnableSwagger2 表示开启Swagger,也可以在配置文件上加@EnableSwagger2

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

我的包结构:

.apis(RequestHandlerSelectors.basePackage("io.sr.modules.tra.app"))  : 是设置扫描的包路径

io.sr.modules.tra.app 它是模糊匹配的,所以我们在创建包还有URL时要避免这种格式

 

result接口:

这一步不是必要的, 只要swagger扫描到@RestController注解的类就会按默认配置生成接口文档

 

三、controller代码:


 
 
  1. @RestController
  2. @RequestMapping( "/user")
  3. @Api(value = "Shop")
  4. public class SpringBootController {
  5. @ApiOperation(value = "获取helloWorld", notes = "简单SpringMVC请求")
  6. @RequestMapping( "/")
  7. String home() {
  8. return "HELLO WORLD";
  9. }
  10. @ApiOperation(value = "获得A+B", notes = "根据url的classNo和url的studentName获得请求参数的字符串相加,RestFul风格的请求")
  11. @ApiImplicitParams({ @ApiImplicitParam(name = "classNo", value = "班级编号", required = true, dataType = "String"),
  12. })
  13. @RequestMapping(value = "/class/{classNo}/to/{studentName}", method = RequestMethod.GET)
  14. String world(@PathVariable("classNo") String classNo, @PathVariable("studentName") String studentName) {
  15. return classNo + " " + studentName;
  16. }
  17. }

访问:http://localhost:8080/swagger-ui.html#/:

四、Swagger注解

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

  • @Api:修饰整个类,描述Controller的作用
  • @ApiOperation:描述一个类的一个方法,或者说一个接口
  • @ApiParam:单个参数描述
  • @ApiModel:用对象来接收参数
  • @ApiProperty:用对象接收参数时,描述对象的一个字段
  • @ApiResponse:HTTP响应其中1个描述
  • @ApiResponses:HTTP响应整体描述
  • @ApiIgnore:使用该注解忽略这个API
  • @ApiError :发生错误返回的信息
  • @ApiImplicitParam:一个请求参数
  • @ApiImplicitParams:多个请求参数

 

作用范围API使用位置
对象属性@ApiModelProperty用在出入参数对象的字段上
协议集描述@Api用于controller类上
协议描述@ApiOperation用在controller的方法上
Response集@ApiResponses用在controller的方法上
Response@ApiResponse用在 @ApiResponses里边
非对象参数集@ApiImplicitParams用在controller的方法上
非对象参数描述@ApiImplicitParam用在@ApiImplicitParams的方法里边
描述返回对象的意义@ApiModel用在返回对象类上

@RequestMapping此注解的推荐配置 
value 
method 
produces

示例:


 
 
  1. @ApiOperation("信息软删除")
  2. @ApiResponses({ @ApiResponse(code = CommonStatus.OK, message = "操作成功"),
  3. @ApiResponse(code = CommonStatus.EXCEPTION, message = "服务器内部异常"),
  4. @ApiResponse(code = CommonStatus.FORBIDDEN, message = "权限不足") })
  5. @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", dataType = "Long", name = "id", value = "信息id", required = true) })
  6. @RequestMapping(value = "/remove.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  7. public RestfulProtocol remove( Long id) {

 
 
  1. @ ApiModelProperty( value = "标题")
  2. private String title;

@ApiImplicitParam

属性取值作用
paramType 查询参数类型
 path以地址的形式提交数据
 query直接跟参数完成自动映射赋值
 body以流的形式提交 仅支持POST
 header参数在request headers 里边提交
 form以form表单的形式提交 仅支持POST
dataType 参数的数据类型 只作为标志说明,并没有实际验证
 Long 
 String 
name 接收参数名
value 接收参数的意义描述
required 参数是否必填
 true必填
 false非必填
defaultValue 默认值

paramType 示例详解

path


 
 
  1. @RequestMapping(value = "/findById1/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  2. @PathVariable(name = "id") Long id

body


 
 
  1. @ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "MessageParam", name = "param", value = "信息参数", required = true) })
  2. @RequestMapping(value = "/findById3", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
  3. @RequestBody MessageParam param
  4. 提交的参数是这个对象的一个json,然后会自动解析到对应的字段上去,也可以通过流的形式接收当前的请求数据,但是这个和上面的接收方式仅能使用一个(用 @RequestBody之后流就会关闭了)

header


 
 
  1. @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "Long", name = "id", value = "信息id", required = true) })
  2. String idstr = request.getHeader( "id");
  3. if (StringUtils.isNumeric(idstr)) {
  4. id = Long.parseLong(idstr);
  5. }

Form


 
 
  1. @ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType = "Long", name = "id", value = "信息id", required = true) })
  2. @RequestMapping(value = "/findById5", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)

详细用法:  https://www.cnblogs.com/softidea/p/6251249.html 

与Spring Security整合设置密码 :  https://blog.csdn.net/rickiyeat/article/details/72639596

代码: https://gitee.com/didispace/swagger-butler

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值