1. 程式人生 > >Spring 4.2.2以上版本和swagger集成方案和踩過的坑

Spring 4.2.2以上版本和swagger集成方案和踩過的坑

pin sele -s ner -h tap width pub equal

因為公司使用的spring版本太高,在集成swagger的時候會存在一些問題,而網上的很多實例大多都是版本比較低的,為了是朋友們少才坑,我這邊將集成的過程記錄一下:

1. 引入spring、swagger的相關jar包(springfox-swagger2、springfox-swagger-ui),在pom.xml中配置:

Xml代碼 技術分享 技術分享技術分享
  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.4.0</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-core</artifactId>
  9. </exclusion>
  10. <exclusion>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-beans</artifactId>
  13. </exclusion>
  14. <exclusion>
  15. <groupId>org.springframework</groupId>
  16. <artifactId>spring-context</artifactId>
  17. </exclusion>
  18. <exclusion>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-context-support</artifactId>
  21. </exclusion>
  22. <exclusion>
  23. <groupId>org.springframework</groupId>
  24. <artifactId>spring-aop</artifactId>
  25. </exclusion>
  26. <exclusion>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-tx</artifactId>
  29. </exclusion>
  30. <exclusion>
  31. <groupId>org.springframework</groupId>
  32. <artifactId>spring-orm</artifactId>
  33. </exclusion>
  34. <exclusion>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-jdbc</artifactId>
  37. </exclusion>
  38. <exclusion>
  39. <groupId>org.springframework</groupId>
  40. <artifactId>spring-web</artifactId>
  41. </exclusion>
  42. <exclusion>
  43. <groupId>org.springframework</groupId>
  44. <artifactId>spring-webmvc</artifactId>
  45. </exclusion>
  46. <exclusion>
  47. <groupId>org.springframework</groupId>
  48. <artifactId>spring-oxm</artifactId>
  49. </exclusion>
  50. </exclusions>
  51. </dependency>
  52. <dependency>
  53. <groupId>io.springfox</groupId>
  54. <artifactId>springfox-swagger-ui</artifactId>
  55. <version>2.4.0</version>
  56. </dependency>
<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的配置類:

Java代碼 技術分享 技術分享技術分享
  1. package com.ml.honghu.swagger.web;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  6. import springfox.documentation.builders.ApiInfoBuilder;
  7. import springfox.documentation.builders.PathSelectors;
  8. import springfox.documentation.builders.RequestHandlerSelectors;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.service.Contact;
  11. import springfox.documentation.spi.DocumentationType;
  12. import springfox.documentation.spring.web.plugins.Docket;
  13. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  14. @EnableWebMvc
  15. @EnableSwagger2
  16. @Configuration
  17. @ComponentScan(basePackages ={"com.ml.honghu.**.rest"})
  18. public class SwaggerConfig {
  19. @Bean
  20. public Docket createRestApi() {
  21. return new Docket(DocumentationType.SWAGGER_2)
  22. .apiInfo(apiInfo())
  23. .select()
  24. .apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))
  25. .paths(PathSelectors.any())
  26. .build();
  27. }
  28. private ApiInfo apiInfo() {
  29. return new ApiInfoBuilder()
  30. .title("接口列表 v1.0")
  31. .description("接口信息")
  32. .termsOfServiceUrl("http://honghu.com")
  33. .contact(new Contact("", "", "HongHu"))
  34. .version("1.1.0")
  35. .build();
  36. }
  37. }
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的相關訪問配置:

Java代碼 技術分享 技術分享技術分享
  1. <mvc:exclude-mapping path="/swagger*/**"/>
  2. <mvc:exclude-mapping path="/v2/**"/>
  3. <mvc:exclude-mapping path="/webjars/**"/>
<mvc:exclude-mapping path="/swagger*/**"/>
<mvc:exclude-mapping path="/v2/**"/>
<mvc:exclude-mapping path="/webjars/**"/>

4. 服務配置項

Java代碼 技術分享 技術分享技術分享
  1. <span style="color: rgb(255, 0, 0);">@Api("區域服務")</span>
  2. @RestController
  3. @RequestMapping(value = "/rest/area")
  4. public class AreaService {
  5. @Autowired
  6. private AreaService areaService;
  7. <span style="color: rgb(255, 0, 0);">@ApiOperation(value = "區域列表", httpMethod = "GET", notes = "區域列表")</span>
  8. @IsLogin
  9. @ResponseBody
  10. @RequestMapping(value = "treeData", method = RequestMethod.GET)
  11. public List<Map<String, Object>> treeData(
  12. <span style="color: rgb(255, 0, 0);">@ApiParam(required = true, value = "區域ID")</span> @RequestParam(required=false) String extId, HttpServletResponse response) {
  13. List<Map<String, Object>> mapList = Lists.newArrayList();
  14. List<Area> list = areaService.findAll();
  15. for (int i=0; i<list.size(); i++){
  16. Area e = list.get(i);
  17. if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
  18. Map<String, Object> map = Maps.newHashMap();
  19. map.put("id", e.getId());
  20. map.put("pId", e.getParentId());
  21. map.put("name", e.getName());
  22. mapList.add(map);
  23. }
  24. }
  25. return mapList;
  26. }
  27. }
@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. 啟動項目,查看結果:


技術分享

願意了解框架技術或者源碼的朋友直接求求交流分享技術:3133806896

分布式的一些解決方案,有願意了解的朋友可以找我們團隊探討

更多詳細源碼參考來源:http://minglisoft.cn/technology


Spring 4.2.2以上版本和swagger集成方案和踩過的坑