Swagger

1、swagger学习

Swagger定义
Swagger同类工具
Swagger和web项目结合
Swagger在公司项目中如何应用

2、Swagger定义

2.1 Swagger子项目

Paste_Image.png

其实swagger是一个比较大的项目,就像spring一样,他下面有很多模块,我们现在只需要使用它的UI模。

3、相关学习博客

4、swagger和springmvc整合

4.1 依赖管理

     
     
  1. <!-- swagger-mvc -->
  2. <dependency>
  3. <groupId>com.mangofactory</groupId>
  4. <artifactId>swagger-springmvc</artifactId>
  5. <version>1.0.2</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.fasterxml.jackson.core</groupId>
  9. <artifactId>jackson-databind</artifactId>
  10. <version>2.4.2</version>
  11. </dependency>
  12. <!-- swagger-mvc -->
4.2 Swagger配置

Swagger的配置最终就是配置一个config类,通过Java编码的方式实现配置:


     
     
  1. @Configuration
  2. @EnableSwagger
  3. public class SwaggerConfig {
  4. private SpringSwaggerConfig springSwaggerConfig;
  5. /**
  6. * Required to autowire SpringSwaggerConfig
  7. */
  8. @Autowired
  9. public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
  10. {
  11. this.springSwaggerConfig = springSwaggerConfig;
  12. }
  13. /**
  14. * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
  15. * framework - allowing for multiple swagger groups i.e. same code base
  16. * multiple swagger resource listings.
  17. */
  18. @Bean
  19. public SwaggerSpringMvcPlugin customImplementation()
  20. {
  21. return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)
  22. .apiInfo(apiInfo())
  23. .includePatterns(".*?");
  24. }
  25. private ApiInfo apiInfo()
  26. {
  27. ApiInfo apiInfo = new ApiInfo(
  28. "My Apps API Title",
  29. "My Apps API Description",
  30. "My Apps API terms of service",
  31. "My Apps API Contact Email",
  32. "My Apps API Licence Type",
  33. "My Apps API License URL");
  34. return apiInfo;
  35. }
  36. }

这个是个模板,里面内容可以自己定义,修改基本上是对apiinfo自定义数据。
在spring中注入Bean:
<context:component-scan base-package="net.shopin.swagger"/>

swagger基本就算是配置好了,剩下的事情就是接口编写和展示了。

引入swagger-UI组件
更改项目文档路径

Paste_Image.png

修改index.html文件中的路径:默认是从连接http://petstore.swagger.io/v2/swagger.json获取API的JSON,我们改为 http://{ip}:{port}/{projectName}/api-docs 的形式,也就ip:端口号/项目名/api-docs

Paste_Image.png

最后就是对controller的书写:


     
     
  1. @Api(value = "product", description = "商品管理", produces = MediaType.APPLICATION_JSON_VALUE)
  2. @Controller
  3. public class ProductController {
  4. @Autowired
  5. private ProductService productService;
  6. @ApiOperation(value = "获得商品信息", notes = "获取商品信息(用于数据同步)", httpMethod = "POST", produces=MediaType.APPLICATION_JSON_VALUE)
  7. @ApiResponses(value = {@ApiResponse(code = 200, message = "商品信息"),
  8. @ApiResponse(code = 201, message = ErrorType.errorCheckToken + "(token验证失败)", response=String.class),
  9. @ApiResponse(code = 202, message = ErrorType.error500 + "(系统错误)",response = String.class)})
  10. @RequestMapping(value = RestUrl.getProduct, method = RequestMethod.POST)
  11. @ResponseBody
  12. public ResultDTO<Products> getProduct(@ApiParam(value = "json参数", required = true) @RequestBody BaseParam param)throws Exception {
  13. return productService.getProduct(param);
  14. }
  15. }
  • @API:表示一个开放的API,可以通过description简明的描述API的功能,produce指定API的mime类型
  • 一个@API下,可以有多个@ApiOperation,表示针对该API的CRUD操作,在ApiOperation Annotation中还可以通过value,notes描述该操作的作用,response描述正常情况下该请求返回的对象类型
  • 在一个@ApiOperation下,可以通过@ApiResponses描述API操作可能出现的异常情况
  • @ApiParam用于描述该API操作接收的参数类型,value用于描述参数,required指明参数是否为必须。
  • @ApiModelProperty中,value指定描述,required指明是否为必须

效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值