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(") .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; <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; } }
啟動專案,檢視結果:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31539957/viewspace-2221577/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP7 以上版本 使用 xdebug 踩坑PHP
- SpringBoot2.6.x及以上版本整合swagger文件Spring BootSwagger
- springboot整合swagger遇到的坑Spring BootSwagger
- 記錄springboot 3.3.5 版本整合 swagger +spring security + jwtSpring BootSwaggerJWT
- Spring Boot整合SwaggerSpring BootSwagger
- Spring Boot整合Springfox Swagger3和簡單應用Spring BootSwagger
- 手把手教你Spring Boot整合Mybatis Plus和Swagger2Spring BootMyBatisSwagger
- Spring Boot 快速整合SwaggerSpring BootSwagger
- 【Spring Boot】快速整合SwaggerSpring BootSwagger
- GeoServer 踩過的坑Server
- Activiti7 與 Spring Boot 及 Spring Security 整合 踩坑記錄Spring Boot
- .NetCore(.NET6)中使用swagger和swagger版本控制NetCoreSwagger
- Spring Boot 容器化踩坑與解決方案(1)Spring Boot
- C/C++ 踩過的坑和防禦式程式設計C++程式設計
- Spring Boot整合swagger使用教程Spring BootSwagger
- Spring Boot 整合 SpringDoc Swagger 3Spring BootSwagger
- Spring Boot系列十九 Spring boot整合 swaggerSpring BootSwagger
- JasperReport 中踩過的坑
- WSL2和CUDA踩坑
- Flutter之Dialog使用和踩坑Flutter
- weex官方文件手冊上問題和踩過的幾個坑。
- Quartz - Spring和Quartz的整合quartzSpring
- 三分鐘搞定 Springboot 和Swagger 整合Spring BootSwagger
- 整合Atomikos、Quartz、Postgresql的踩坑日記quartzSQL
- 解析資料踩過的坑
- 你踩過flutter的坑嗎Flutter
- Maven依賴版本更新踩坑Maven
- 親自踩過的vue的坑Vue
- MyBatis和Spring整合的奧祕MyBatisSpring
- Spring Boot 整合 Swagger 構建介面文件Spring BootSwagger
- mybatis plus +springboot +jsp整合踩坑指南MyBatisSpring BootJS
- 利用swagger和API Version實現api版本控制SwaggerAPI
- wepy小程式踩過的坑(1)
- 安裝 Laravel/horizon 踩過的坑Laravel
- Compose 延遲列表踩過的坑
- uniapp之那些年踩過的坑APP
- Electron 截圖踩坑和優化集合優化
- Xcode10和iOS12踩坑XCodeiOS