因为公司使用的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包,如绿色标志。

2. 编写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();
}
}

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

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

<mvc:exclude-mapping path="/swagger*/**"/>
<mvc:exclude-mapping path="/v2/**"/>
<mvc:exclude-mapping path="/webjars/**"/>

4. 服务配置项

<span style="color: #ff0000;">@Api("区域服务")</span>
@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;
}
}

4. 启动项目,查看结果:

到此结束!!源码来源

Spring 4.2.2以上版本和swagger集成方案和踩过的坑的更多相关文章

  1. Spring Boot拦截器实现并和swagger集成后使用拦截器的配置问题

    1. 定义拦截器 LoginInterceptor LoginInterceptor.java是整个登录认证模块中的核心类之一,它实现了HandlerInterceptor类,由它来拦截并过滤到来的每 ...

  2. 【Swagger】可能是目前最好的 Spring Boot 集成 swagger 的方案

    [Swagger]可能是目前最好的Spring Boot集成 swagger 的方案 ![](https://img2018.cnblogs.com/blog/746311/201909/746311 ...

  3. Spring Boot 2.x基础教程:Swagger接口分类与各元素排序问题详解

    之前通过Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档一文,我们学习了如何使用Swagger为Spring Boot项目自动生成API文档,有不少用户留言问了关于文档 ...

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

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

  5. Spring boot中使用springfox来生成Swagger Specification小结

    Rest接口对应Swagger Specification路径获取办法: 根据location的值获取api   json描述文件 也许有同学会问,为什么搞的这么麻烦,api json描述文件不就是h ...

  6. Spring官网下载各版本jar包

    1:浏览器输入官网地址:http://spring.io/projects 2:点击如图树叶页面按钮.  3:点击如图小猫图片按钮.  4:查找downloading spring artifacts ...

  7. Spring Cloud 升级最新 Finchley 版本,踩坑指南!

    https://blog.csdn.net/youanyyou/article/details/81530240 Spring Cloud 升级最新 Finchley 版本,踩了所有的坑! 2018年 ...

  8. Spring MVC学习总结(8)——Swagger入门详解

    前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...

  9. Spring Cloud 升级最新 Greenwich 版本,舒服了~

    去年将 Spring Cloud 升级到了 Finchley 版本: Spring Cloud 升级最新 Finchley 版本,踩了所有的坑! 这个大版本栈长是踩了非常多的坑啊,帮助了不少小伙伴. ...

随机推荐

  1. 04、DAT图像文件

    DAT是芯片的原始扫描图像,如下图: 注:这两张图来自<Bayesian Inference for Gene Expression and Proteomics>.A是U95Av2芯片的 ...

  2. vue.js的v-bind

    v-bind v-bind  主要用于属性绑定, html中的标签内: <div class="control-group"> <label class=&quo ...

  3. JavaSE--基础知识

    Java基础知识 一.基础知识 1.java命名规则 由26个英文字母大小写,0-9 ,_或 $ 组成 数字不可以开头. 不可以使用关键字和保留字,但能包含关键字和保留字. Java中严格区分大小写, ...

  4. RabbitMQ入门教程(二):简介和基本概念

    原文:RabbitMQ入门教程(二):简介和基本概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn ...

  5. 剑指offer-5:十进制转二进制

    一.二进制中‘1’的个数 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. public class Solution { public int NumberOf1(int n) { ...

  6. 纯CSS绘制3D立方体

    本篇记录的是使用CSS3绘制3D立方体,并旋转起来. 我的思路: 1️⃣ 首先,用div元素画6个正方形摞在一起放在画布中间.为了区分,分别给每个div选择了不同的颜色,并且设置为半透明方便透视. 2 ...

  7. 为什么对华为不拍Arm?

    华为可以靠着现有的 ARMv8 授权坚持很长一段时间,足以等到这波科技禁运结束. 今天,华为在美国遭遇的科技禁运上升到了全球新高度. 据 BBC 报道,由软银全资拥有的英国技术公司 Arm 向员工发出 ...

  8. DSP学习资料:基于6U VPX的 XC7VX690T+C6678的双FMC接口雷达通信处理板

    基于6U VPX的 XC7VX690T+C6678的双FMC接口雷达通信处理板   一.板卡概述 高性能VPX信号处理板基于标准6U VPX架构,提供两个标准FMC插槽,适用于电子对抗或雷达信号等领域 ...

  9. 模拟赛小结:2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest

    2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest 2019年10月11日 15:30-20:30(Solved 6,Penalty 740 ...

  10. (转) weblogic 域,管理服务器,受管服务器,集群和机器的基本知识

    weblogic 域,管理服务器,受管服务器,集群和机器的基本知识 1.域(Domain) •它是什么? –是一个逻辑上管理的WebLogic Server组,这些组从管理上当作一个整体来操作 •域里 ...