因为公司使用的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. 服务配置项
@Api("区域服务") @RestController @RequestMapping(value = "/rest/area") public class AreaService { @Autowired private AreaService areaService; @ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表") @IsLogin @ResponseBody @RequestMapping(value = "treeData", method = RequestMethod.GET) public List<Map<String, Object>> treeData( @ApiParam(required = true, value = "区域ID") @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. 启动项目,查看结果:
到此结束!!
相关推荐
下载jar包,就找安楠大叔,jar包多多,想要打包下载的,请私聊我!
本资源是一个最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 的一个简单的 demo,也是博客《最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例》的配套示例项目,该博客地址是:...
spring-framework-4.2.2.zip 进行spring开发必不可少的文件 该压缩包解压后的目录为: |----lib 所使用到的jar文件 |----docs 帮助文档 |----schema 结构 包含WEB开发程序所使用的spring jar文件、包含spring开发...
spring-framework-4.2.2.RELEASE最近spring帮助文档
ssh整合_struts2.3.24_spring4.2.2_hib5.0.2
赠送jar包:spring-test-4.2.2.RELEASE.jar; 赠送原API文档:spring-test-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-test-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-test-4.2.2.RELEASE....
分两个包(当前第二个)spring4.2.2最新最稳定版
spring-framework-4.2.2.RELEASE官方完整包加官方文档
官网下载下来的,上传到国内可以比较快下载; 官方的下载地址是: http://repo.spring.io/libs-release-local/org/springframework/ 不过比较慢
spring-core-4.2.2.RELEASE.jar (spring 4.4.2的核心jar包),Java开发中spring框架开发必须的依赖包。
思考spring4.2.2 + springmvc4.2.2 + hibernate4.3.11版本
赠送jar包:spring-context-4.2.2.RELEASE.jar; 赠送原API文档:spring-context-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-context-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-context-...
spring-core-4.2.2.RELEASE-javadoc.jar (spring 4.4.2的文档doc,即为spring的源码注释),导入到eclipse中,就可以查看spring的源码注释,当调用spring的类或者方法时就会显示注释信息。
赠送jar包:spring-tx-4.2.2.RELEASE.jar; 赠送原API文档:spring-tx-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-tx-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-tx-4.2.2.RELEASE.pom; ...
赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...
The type org.springframework.dao.DataAccessException cannot be resolved. It is indirectly referenced from required .class files 解决方法: 导入spring-tx-4.2.2.RELEASE.jar包。
Struts 2.3.25 + spring-4.2.2 + hibernate-5.0.2 Apache Struts Struts 2.3.24.1 has been released on 24 september 2015. http://struts.apache.org/download.cgi#struts23241 spring-framework-4.2.2.RELEASE-...
spring-framework-4.2.2.zip 进行spring开发必不可少的文件 该压缩包解压后的目录为: |----lib 所使用到的jar文件 |----docs 帮助文档 |----schema 结构 包含WEB开发程序所使用的spring jar文件、包含spring开发...
赠送jar包:spring-oxm-4.2.2.RELEASE.jar; 赠送原API文档:spring-oxm-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-oxm-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-oxm-4.2.2.RELEASE.pom;...