Spring 4.2.2以上版本和swagger集成方案和踩过的坑
因为公司使用的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集成方案和踩过的坑的更多相关文章
- Spring Boot拦截器实现并和swagger集成后使用拦截器的配置问题
1. 定义拦截器 LoginInterceptor LoginInterceptor.java是整个登录认证模块中的核心类之一,它实现了HandlerInterceptor类,由它来拦截并过滤到来的每 ...
- 【Swagger】可能是目前最好的 Spring Boot 集成 swagger 的方案
[Swagger]可能是目前最好的Spring Boot集成 swagger 的方案 ——Swagger入门详解
前言 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容,见参考资料. S ...
- Spring Cloud 升级最新 Greenwich 版本,舒服了~
去年将 Spring Cloud 升级到了 Finchley 版本: Spring Cloud 升级最新 Finchley 版本,踩了所有的坑! 这个大版本栈长是踩了非常多的坑啊,帮助了不少小伙伴. ...
随机推荐
- 【转帖】AMD:未向合资企业THATIC发放后续芯片设计授权
AMD:未向合资企业THATIC发放后续芯片设计授权 https://www.cnbeta.com/articles/tech/854193.htm 海光和兆芯的CPU 都不靠谱啊. 在台北电脑展(C ...
- Luogu P4095 [HEOI2013]Eden的新背包问题
题目 求出从前往后的背包\(f_{i,j}\)和从后往前的背包\(F_{i,j}\). 那么对于询问\((d,e)\),答案就是\(\max\limits_{i=0}^e f_{d-1,i}+F_{d ...
- 利用Kruskal算法求最小生成树解决聪明的猴子问题 -- 数据结构
题目:聪明的猴子 链接:https://ac.nowcoder.com/acm/problem/19964 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个 ...
- 如何将Numpy加速700倍?用 CuPy 呀
如何将Numpy加速700倍?用 CuPy 呀 作为 Python 语言的一个扩展程序库,Numpy 支持大量的维度数组与矩阵运算,为 Python 社区带来了很多帮助.借助于 Numpy,数据科学家 ...
- Maven出错的问题处理
1:使用Maven部署dubbox.jar包到maven本地仓库 由于Dubbox的jar包并没有部署到Maven的中央仓库中,大家在Maven的中央仓库中可以查找到Dubbo的最终版本是2.5.3 ...
- thinkphp5 安装
thinkphp 5开始可以使用composer安装 所以在安装thinkphp5.1之前,我们先安装composer ,下载地址:https://www.phpcomposer.com/ 安装完co ...
- JVM 之类加载器
一.什么是 JVM JVM(Java Virtual Machine)是一个可以执行 Java 字节码文件(即 .class 文件)的虚拟机进程.当 Java 源文件能被成功编译成 .class 文件 ...
- nginx服务报403错误的解决方法
1.可能是文件权限问题,看一下网站所在的文件夹权限,用户组和用户名是否属于www:www,因为nginx.conf顶头写的是user www:www
- vue-mixins和vue高阶组件
我们在开发过程中,因为需求的变更,往往会遇见对现有组件的改造和扩展. 那么我们有什么方法对现有组件进行改造和扩展呢? 常见的我们可以使用mixins方式 下面就让我们来看一下怎么使用mixins方式对 ...
- 使用switchshow/supportshow命令确认Brocade交换机型号(转载)
switchshow命令(或supportshow日志)中的switchType是以数字来代表不同的交换机型号,完整的对应表格如下: Switchtype EMC / Brocade名称 / 端口 / ...