我可以: 邀请好友来看>>
ZOL星空(中国) > 技术星空(中国) > Java技术星空(中国) > Spring 4.2.2以上版本和swagger集成方案和踩过的坑
帖子很冷清,卤煮很失落!求安慰
返回列表
签到
手机签到经验翻倍!
快来扫一扫!

Spring 4.2.2以上版本和swagger集成方案和踩过的坑

117浏览 / 1回复

皮皮111

皮皮111

0
精华
48
帖子

等  级:Lv.3
经  验:898
  • Z金豆: 0

    千万礼品等你来兑哦~快点击这里兑换吧~

  • 城  市:北京
  • 注  册:2017-10-19
  • 登  录:2017-11-22
发表于 2017-11-06 11:57:48
电梯直达 确定
楼主

因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了是朋友们少才坑,我这边将集成的过程记录一下:

1. 引入spring、swagger的相关jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:

 

Xml代码  Spring 4.2.2以上版本和swagger集成方案和踩过的坑

  1. <dependency>  

  2.             <groupId>io.springfoxgroupId>  

  3.             <artifactId>springfox-swagger2artifactId>  

  4.             <version>2.4.0version>  

  5.             <exclusions>  

  6.                 <exclusion>  

  7.                     <groupId>org.springframeworkgroupId>  

  8.                     <artifactId>spring-coreartifactId>  

  9.                 exclusion>  

  10.                 <exclusion>  

  11.                     <groupId>org.springframeworkgroupId>  

  12.                     <artifactId>spring-beansartifactId>  

  13.                 exclusion>  

  14.                 <exclusion>  

  15.                     <groupId>org.springframeworkgroupId>  

  16.                     <artifactId>spring-contextartifactId>  

  17.                 exclusion>  

  18.                 <exclusion>  

  19.                     <groupId>org.springframeworkgroupId>  

  20.                     <artifactId>spring-context-supportartifactId>  

  21.                 exclusion>  

  22.                 <exclusion>  

  23.                     <groupId>org.springframeworkgroupId>  

  24.                     <artifactId>spring-aopartifactId>  

  25.                 exclusion>  

  26.                 <exclusion>  

  27.                     <groupId>org.springframeworkgroupId>  

  28.                     <artifactId>spring-txartifactId>  

  29.                 exclusion>  

  30.                 <exclusion>  

  31.                     <groupId>org.springframeworkgroupId>  

  32.                     <artifactId>spring-ormartifactId>  

  33.                 exclusion>  

  34.                 <exclusion>  

  35.                     <groupId>org.springframeworkgroupId>  

  36.                     <artifactId>spring-jdbcartifactId>  

  37.                 exclusion>  

  38.                 <exclusion>  

  39.                     <groupId>org.springframeworkgroupId>  

  40.                     <artifactId>spring-webartifactId>  

  41.                 exclusion>  

  42.                 <exclusion>  

  43.                     <groupId>org.springframeworkgroupId>  

  44.                     <artifactId>spring-webmvcartifactId>  

  45.                 exclusion>  

  46.                 <exclusion>  

  47.                     <groupId>org.springframeworkgroupId>  

  48.                     <artifactId>spring-oxmartifactId>  

  49.                 exclusion>  

  50.             exclusions>  

  51.         dependency>  

  52.         <dependency>  

  53.             <groupId>io.springfoxgroupId>  

  54.             <artifactId>springfox-swagger-uiartifactId>  

  55.             <version>2.4.0version>  

  56.         dependency>  

 

 

提醒: 特别注意,springfox-swagger2在集成的时候,已经引入了spring的相关jar,特别是spring-context、spring-context-support的版本和项目中使用的版本完全不一致,项目在启动的时候出现很多包冲突的问题,这边在引入pom.xml文件的时候过滤掉了spring的相关jar包,如绿色标志。

 

2. 编写Swagger的配置类:

 

Java代码  Spring 4.2.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(bbsePackages ={"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.bbsePackage("com.ml.honghu"))  

  28.                 .paths(PathSelectors.any())  

  29.                 .build();  

  30.     }  

  31.   

  32.     private ApiInfo apiInfo() {  

  33.         return new ApiInfoBuilder()  

  34.                 .title("接口列表 v1.0")  

  35.                 .descripqion("接口信息")  

  36.                 .termsOfServiceUrl("http://honghu.com")  

  37.                 .contact(new Contact("""""HongHu"))  

  38.                 .version("1.1.0")  

  39.                 .build();  

  40.     }  

  41. }  

 提醒:注意红色标注的地方

 

 

3. 在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相关访问配置:

 

Java代码  Spring 4.2.2以上版本和swagger集成方案和踩过的坑

  1. "/swagger*/**"/>  

  2. "/v2/**"/>  

  3. "/webjars/**"/>  

 

 

4. 服务配置项

 

Java代码  Spring 4.2.2以上版本和swagger集成方案和踩过的坑

  1. "color: #ff0000;">@Api("区域服务")  

  2. @RestController  

  3. @RequestMapping(value = "/rest/area")  

  4. public class AreaService  {  

  5.   

  6.     @Autowired  

  7.     private AreaService areaService;  

  8.       

  9.     "color: #ff0000;">@ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表")  

  10.     @IsLogin  

  11.     @ResponseBody  

  12.     @RequestMapping(value = "treeData", method = RequestMethod.GET)  

  13.     public List> treeData(  

  14.             "color: #ff0000;">@ApiParam(required = true, value = "区域ID")  @RequestParam(required=false) String extId, HttpServletResponse response) {  

  15.         List> mapList = Lists.newArrayList();  

  16.         ListSpring 4.2.2以上版本和swagger集成方案和踩过的坑
     

    到此结束!!

皮皮111

皮皮111


精华

帖子

等  级:Lv.3
经  验:898
发表于 2017-11-06 14:07:32 1楼

有兴趣可以加企鹅2147775633,希望可以帮助更多的好学者。

高级模式
星空(中国)精选大家都在看24小时热帖7天热帖大家都在问最新回答

针对ZOL星空(中国)您有任何使用问题和建议 您可以 联系星空(中国)管理员查看帮助  或  给我提意见

快捷回复 APP下载 返回列表