因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了使朋友们少踩坑,我这边将集成的过程记录一下:愿意了解源码的朋友直接求求交流分享技术二一四七七七五六三三

  1. 引入spring、swagger的相关jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:
<dependency>  <groupId>io.springfox</groupId>  <artifactId>springfox-swagger2</artifactId>  <version>2.4.0</version>  <exclusions>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-core</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-beans</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-context-support</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-aop</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-tx</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-orm</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-jdbc</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-web</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-webmvc</artifactId>  </exclusion>  <exclusion>  <groupId>org.springframework</groupId>  <artifactId>spring-oxm</artifactId>  </exclusion>  </exclusions>  </dependency>  <dependency>  <groupId>io.springfox</groupId>  <artifactId>springfox-swagger-ui</artifactId>  <version>2.4.0</version>  </dependency>
复制代码

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

  1. 编写Swagger的配置类:
package com.ml.honghu.swagger.web;  import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;  import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;  @EnableWebMvc
@EnableSwagger2
@Configuration
@ComponentScan(basePackages ={"com.ml.honghu.**.rest"})
public class SwaggerConfig {  @Bean  public Docket createRestApi() {  return new Docket(DocumentationType.SWAGGER_2)  .apiInfo(apiInfo())  .select()  .apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))  .paths(PathSelectors.any())  .build();  }  private ApiInfo apiInfo() {  return new ApiInfoBuilder()  .title("接口列表 v1.0")  .description("接口信息")  .termsOfServiceUrl("http://honghu.com")  .contact(new Contact("", "", "HongHu"))  .version("1.1.0")  .build();  }
}
复制代码
  1. 在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相关访问配置:
<mvc:exclude-mapping path="/swagger*/**"/>
<mvc:exclude-mapping path="/v2/**"/>
<mvc:exclude-mapping path="/webjars/**"/>
复制代码
  1. 服务配置项
@Api("区域服务")
@RestController
@RequestMapping(value = "/rest/area")
public class AreaService  {  @Autowired  private AreaService areaService;  <span style="color: #ff0000;">@ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表")</span>  @IsLogin  @ResponseBody  @RequestMapping(value = "treeData", method = RequestMethod.GET)  public List<Map<String, Object>> treeData(  <span style="color: #ff0000;">@ApiParam(required = true, value = "区域ID")</span>  @RequestParam(required=false) String extId, HttpServletResponse response) {  List<Map<String, Object>> mapList = Lists.newArrayList();  List<Area> list = areaService.findAll();  for (int i=0; i<list.size(); i++){  Area e = list.get(i);  if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){  Map<String, Object> map = Maps.newHashMap();  map.put("id", e.getId());  map.put("pId", e.getParentId());  map.put("name", e.getName());  mapList.add(map);  }  }  return mapList;  }
}
复制代码

启动项目,查看结果:

资料和源码来源

转载于:https://juejin.im/post/5bfb8895e51d450c487d13f4

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

  1. (十二)java版电子商务spring cloud分布式微服务- Spring 4.2.2以上版本和swagger集成方案和踩过的坑...

    电子商务平台源码请加企鹅求求:一零三八七七四六二六.因为公司使用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比较低的,为了是朋友们少才坑,我这边将集成 ...

  2. Spring Boot 2.x基础教程:Swagger静态文档的生成

    前言 通过之前的两篇关于Swagger入门以及具体使用细节的介绍之后,我们已经能够轻松地为Spring MVC的Web项目自动构建出API文档了.如果您还不熟悉这块,可以先阅读: Spring Boo ...

  3. 厉害了,Spring Cloud Alibaba 发布 GA 版本!

    小马哥 & Josh Long 喜欢写一首诗一般的代码,更喜欢和你共同 code review,英雄的相惜,犹如时间沉淀下来的对话,历久方弥新. 相见如故,@杭州. 4 月 18 日,Josh ...

  4. Myeclipse的standard、pro、spring、Blue、Bling版本功能差别

    Myeclipse有standard.pro.spring.Blue.Bling版本,下面是这些版本的功能上的差别 各版本区别:http://www.myeclipseide.com/module-h ...

  5. Spring Cloud 升级最新 Finchley 版本,踩了所有的坑

    转载自   Spring Cloud 升级最新 Finchley 版本,踩了所有的坑 Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Bo ...

  6. Spring Boot 与 Java 对应版本,以下表格由官方网站总结。

    Spring Boot 与 Java 对应版本,以下表格由官方网站总结. 官网:https://spring.io/projects/spring-boot#learn https://docs.sp ...

  7. 在spring官网上下载历史版本的spring插件,springsource-tool-suite

    在spring官网上下载历史版本的spring插件,springsource-tool-suite 如何为自己的eclipse下载历史版本的sts呢?拼下载的url. 首先,鼠标右键可下载的sts链接 ...

  8. Spring学习总结(22)——Spring-framework-bom解决spring的不同模块依赖版本不同问题

    Spring不同模块或者与外部进行集成时,依赖处理就需要各自对应版本号.比如,较新spring与较老的quartz,它们集成就会遇到问题,给搭建和升级带来不便.因此Spring IO Platform ...

  9. Spring学习总结(22)——spring-framework-bom解决spring的不同模块依赖版本不同问题...

    Spring不同模块或者与外部进行集成时,依赖处理就需要各自对应版本号.比如,较新spring与较老的quartz,它们集成就会遇到问题,给搭建和升级带来不便.因此Spring IO Platform ...

最新文章

  1. uva 11069 A Graph Problem
  2. linux pwm 调屏_基于嵌入式Linux的LCD背光调节及驱动的实现
  3. 自定义控件(视图)2期笔记13:View的滑动冲突之 内部拦截法
  4. 使用循环链表实现一个通讯录的管理程序_【LeetCode链表题型总结】
  5. 15年3月c语言试卷,2015年3月计算机二级C语言试卷及答案..doc
  6. IE发现新的零日攻击漏洞 用户可采取缓解措施
  7. VC++调试错误的解决方案
  8. U盘安装系统 从此告别光盘
  9. C语言中对程序设计的理解,浅论《C语言程序设计》的学习方法
  10. html页面设置document类型,解析网页头部代码:Html Document
  11. 数据类型不一致: 应为 NUMBER, 但却获得 BINARY
  12. MPP文件怎么打开?五种方法总有最合适!
  13. CodeWars刷题笔记
  14. 大学的c语言课程难度,大学挂科率最高的4门课程,学霸也担心挂科,有你学过的课程吗?...
  15. python 获取list特定元素下标
  16. opencv简易数字识别
  17. 91461452469259f108dee0593bece4cb
  18. Android各厂商Rom包解压方式
  19. CppCheck使用说明
  20. 计算机软件商标,计算机软件商标的类别号 是多少?

热门文章

  1. 云计算已成创新基础设施,三大暗流左右未来“云市场”
  2. USRP E310启用SSH的X11 Forwarding功能
  3. javascript函数、对象及变量、正则表达式(7.19)
  4. freemarker-ide eclipse安装地址 安装方法 页面静态化
  5. .net面试问题汇总(转)
  6. 玩转GridView
  7. 锐捷大中型企业VLAN网络方案
  8. 从架构特点到功能缺陷,重新认识分析型分布式数据库
  9. OCS 企业版图解详细部署
  10. js 判断数据类型的几种方法