免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5728 | 回复: 1
打印 上一主题 下一主题

Spring 4.2.2以上版本和swagger集成方案和踩过的坑 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-06-15 14:54 |只看该作者 |倒序浏览
因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了是朋友们少才坑,我这边将集成的过程记录一下:
1. 引入spring、swagger的相关jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:
  1. <dependency>  
  2.             <groupId>io.springfox</groupId>  
  3.             <artifactId>springfox-swagger2</artifactId>  
  4.             <version>2.4.0</version>  
  5.             <exclusions>  
  6.                 <exclusion>  
  7.                     <groupId>org.springframework</groupId>  
  8.                     <artifactId>spring-core</artifactId>  
  9.                 </exclusion>  
  10.                 <exclusion>  
  11.                     <groupId>org.springframework</groupId>  
  12.                     <artifactId>spring-beans</artifactId>  
  13.                 </exclusion>  
  14.                 <exclusion>  
  15.                     <groupId>org.springframework</groupId>  
  16.                     <artifactId>spring-context</artifactId>  
  17.                 </exclusion>  
  18.                 <exclusion>  
  19.                     <groupId>org.springframework</groupId>  
  20.                     <artifactId>spring-context-support</artifactId>  
  21.                 </exclusion>  
  22.                 <exclusion>  
  23.                     <groupId>org.springframework</groupId>  
  24.                     <artifactId>spring-aop</artifactId>  
  25.                 </exclusion>  
  26.                 <exclusion>  
  27.                     <groupId>org.springframework</groupId>  
  28.                     <artifactId>spring-tx</artifactId>  
  29.                 </exclusion>  
  30.                 <exclusion>  
  31.                     <groupId>org.springframework</groupId>  
  32.                     <artifactId>spring-orm</artifactId>  
  33.                 </exclusion>  
  34.                 <exclusion>  
  35.                     <groupId>org.springframework</groupId>  
  36.                     <artifactId>spring-jdbc</artifactId>  
  37.                 </exclusion>  
  38.                 <exclusion>  
  39.                     <groupId>org.springframework</groupId>  
  40.                     <artifactId>spring-web</artifactId>  
  41.                 </exclusion>  
  42.                 <exclusion>  
  43.                     <groupId>org.springframework</groupId>  
  44.                     <artifactId>spring-webmvc</artifactId>  
  45.                 </exclusion>  
  46.                 <exclusion>  
  47.                     <groupId>org.springframework</groupId>  
  48.                     <artifactId>spring-oxm</artifactId>  
  49.                 </exclusion>  
  50.             </exclusions>  
  51.         </dependency>  
  52.         <dependency>  
  53.             <groupId>io.springfox</groupId>  
  54.             <artifactId>springfox-swagger-ui</artifactId>  
  55.             <version>2.4.0</version>  
  56.         </dependency>  
复制代码
提醒: 特别注意,springfox-swagger2在集成的时候,已经引入了spring的相关jar,特别是spring-context、spring-context-support的版本和项目中使用的版本完全不一致,项目在启动的时候出现很多包冲突的问题,这边在引入pom.xml文件的时候过滤掉了spring的相关jar包,如绿色标志。
2. 编写Swagger的配置类:
  1. package com.ml.honghu.swagger.web;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.ComponentScan;  
  5. import org.springframework.context.annotation.Configuration;  
  6. import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
  7.   
  8. import springfox.documentation.builders.ApiInfoBuilder;  
  9. import springfox.documentation.builders.PathSelectors;  
  10. import springfox.documentation.builders.RequestHandlerSelectors;  
  11. import springfox.documentation.service.ApiInfo;  
  12. import springfox.documentation.service.Contact;  
  13. import springfox.documentation.spi.DocumentationType;  
  14. import springfox.documentation.spring.web.plugins.Docket;  
  15. import springfox.documentation.swagger2.annotations.EnableSwagger2;  
  16.   
  17. @EnableWebMvc  
  18. @EnableSwagger2  
  19. @Configuration  
  20. @ComponentScan(basePackages ={"com.ml.honghu.**.rest"})  
  21. public class SwaggerConfig {  
  22.     @Bean  
  23.     public Docket createRestApi() {  
  24.         return new Docket(DocumentationType.SWAGGER_2)  
  25.                 .apiInfo(apiInfo())  
  26.                 .select()  
  27.                 .apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))  
  28.                 .paths(PathSelectors.any())  
  29.                 .build();  
  30.     }  
  31.   
  32.     private ApiInfo apiInfo() {  
  33.         return new ApiInfoBuilder()  
  34.                 .title("接口列表 v1.0")  
  35.                 .description("接口信息")  
  36.                 .termsOfServiceUrl("http://honghu.com")  
  37.                 .contact(new Contact("", "", "HongHu"))  
  38.                 .version("1.1.0")  
  39.                 .build();  
  40.     }  
  41. }  
复制代码
3. 在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相关访问配置:
  1. <mvc:exclude-mapping path="/swagger*/**"/>   
  2. <mvc:exclude-mapping path="/v2/**"/>   
  3. <mvc:exclude-mapping path="/webjars/**"/>   
复制代码
4. 服务配置项
  1. <span style="color: #ff0000;">@Api("区域服务")</span>  
  2. @RestController  
  3. @RequestMapping(value = "/rest/area")  
  4. public class AreaService  {  
  5.   
  6.     @Autowired  
  7.     private AreaService areaService;  
  8.       
  9.     <span style="color: #ff0000;">@ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表")</span>  
  10.     @IsLogin  
  11.     @ResponseBody  
  12.     @RequestMapping(value = "treeData", method = RequestMethod.GET)  
  13.     public List<Map<String, Object>> treeData(  
  14.             <span style="color: #ff0000;">@ApiParam(required = true, value = "区域ID")</span>  @RequestParam(required=false) String extId, HttpServletResponse response) {  
  15.         List<Map<String, Object>> mapList = Lists.newArrayList();  
  16.         List<Area> list = areaService.findAll();  
  17.         for (int i=0; i<list.size(); i++){  
  18.             Area e = list.get(i);  
  19.             if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){  
  20.                 Map<String, Object> map = Maps.newHashMap();  
  21.                 map.put("id", e.getId());  
  22.                 map.put("pId", e.getParentId());  
  23.                 map.put("name", e.getName());  
  24.                 mapList.add(map);  
  25.             }  
  26.         }  
  27.         return mapList;  
  28.     }  
  29. }  
复制代码
4. 启动项目,查看结果:

到此结束!!

论坛徽章:
0
2 [报告]
发表于 2018-06-15 14:54 |只看该作者
喜欢的朋友可以持续关注更新文章!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP