springMVC整合swagger

swagger是什么:

[plain] view plain copy
 print?
  1. Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法和一些常见问题。如果想深入分析项目源码,了解更多内容,见参考资料。  
  2. Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。  


将swagger集成到springmvc项目中去:

首先添加swagger依赖,作者用的maven管理:

  1. <!-- swagger -->  
  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-core</artifactId>  
  10.     <version>2.5.1</version>  
  11. </dependency>  
  12. <dependency>  
  13.     <groupId>com.fasterxml.jackson.core</groupId>  
  14.     <artifactId>jackson-databind</artifactId>  
  15.     <version>2.5.1</version>  
  16. </dependency>  
  17. <dependency>  
  18.     <groupId>com.fasterxml.jackson.core</groupId>  
  19.     <artifactId>jackson-annotations</artifactId>  
  20.     <version>2.5.1</version>  
  21. </dependency>   

创建自定义swagger初始化配置文件:

  1. package com.yrok.swagger;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.annotation.Bean;  
  5.   
  6. import com.mangofactory.swagger.configuration.SpringSwaggerConfig;  
  7. import com.mangofactory.swagger.models.dto.ApiInfo;  
  8. import com.mangofactory.swagger.plugin.EnableSwagger;  
  9. import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;  
  10.   
  11. @EnableSwagger  
  12. public class SwaggerConfig {  
  13.   
  14.     private SpringSwaggerConfig springSwaggerConfig;  
  15.   
  16.     /** 
  17.      * Required to autowire SpringSwaggerConfig 
  18.      */  
  19.     @Autowired  
  20.     public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)  
  21.     {  
  22.         this.springSwaggerConfig = springSwaggerConfig;  
  23.     }  
  24.   
  25.     /** 
  26.      * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc 
  27.      * framework - allowing for multiple swagger groups i.e. same code base 
  28.      * multiple swagger resource listings. 
  29.      */  
  30.     @Bean  
  31.     public SwaggerSpringMvcPlugin customImplementation()  
  32.     {  
  33.         return new SwaggerSpringMvcPlugin(this.springSwaggerConfig)  
  34.                 .apiInfo(apiInfo())  
  35.                 .includePatterns(".*?");  
  36.     }  
  37.   
  38.     private ApiInfo apiInfo()  
  39.     {  
  40.         ApiInfo apiInfo = new ApiInfo(  
  41.                 "springmvc搭建swagger",  
  42.                 "spring-API swagger测试",  
  43.                 "My Apps API terms of service",  
  44.                 "534560449@qq.com",  
  45.                 "web app",  
  46.                 "My Apps API License URL");  
  47.         return apiInfo;  
  48.     }  
  49. }  

将swagger配置类及依赖SpringSwaggerConfig加载到spring容器中:

  1. <!-- 启用MVC注解 -->  
  2. <mvc:annotation-driven />  
  3. <!-- 将 springSwaggerConfig加载到spring容器 -->  
  4. <bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />  
  5. <!-- 将自定义的swagger配置类加载到spring容器 -->  
  6. <bean class="com.yrok.swagger.SwaggerConfig" />  

配置相关controller的api:

  1. package com.yrok.controller;  
  2.   
  3. import javax.annotation.Resource;  
  4.   
  5. import org.springframework.stereotype.Controller;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RequestParam;  
  8. import org.springframework.web.bind.annotation.ResponseBody;  
  9.   
  10. import com.wordnik.swagger.annotations.Api;  
  11. import com.wordnik.swagger.annotations.ApiOperation;  
  12. import com.wordnik.swagger.annotations.ApiParam;  
  13. import com.yrok.entity.User;  
  14. import com.yrok.service.UserService;  
  15. @Api(value="user")  
  16. @Controller  
  17. @RequestMapping(value="/user")  
  18. public class UserController {  
  19.   
  20.     @Resource  
  21.     UserService userService;  
  22.   
  23.     @RequestMapping(value = "/getUser")  
  24.     @ResponseBody  
  25.     @ApiOperation(value="根据ID获取用户信息",httpMethod="GET",notes="get user by id",response=User.class)  
  26.     public User getUser(@ApiParam(required=true,value="用户ID",name="userId")@RequestParam(value="userId")Integer userId) {  
  27.         return userService.getUserByID(userId);  
  28.     }  
  29.       
  30. }  

和Swagger UI的集成:

在GitHub上将swaggerui下载,地址:https://github.com/swagger-api/swagger-ui

解压后将dist文件夹中所有的文件拷贝到webapp/swagger这里的swagger是作者自定义的你可以写为自己创建的目录。

修改index.html中的 http://petstore.swagger.wordnik.com/v2/swagger.json修改为自己项目路径+api-docs,例如:

http://localhost:8080/SSMSwagger/api-docs:SSMSwagger为项目名称。

spring-mvc.xml中过滤掉swagger-ui的文件:

  1. <!-- 静态资源文件,不会被Spring MVC拦截 -->  
  2. <mvc:resources mapping="/swagger/**" location="/swagger/" />  

到这里基本完成了springmvc和swagger的整合,下面是测试结果:

在浏览器中访问:http://localhost:8080/SSMSwagger/swagger/index.html#/user/getUser

到此,本篇文章已经结束,下面分享其他资料和参考:

参考地址:
官网:http://swagger.io/
GitHub:
swagger-springmvc:https://github.com/martypitt/swagger-springmvc
swagger-ui:https://github.com/swagger-api/swagger-ui
swagger-core:https://github.com/swagger-api/swagger-core
swagger-spec:https://github.com/swagger-api/swagger-spec

原文地址:https://www.cnblogs.com/justuntil/p/7211302.html