因为公司应用的spring版本太高,在集成swagger的时候会存在一些问题,而网上的很多实例大多都是版本比拟低的,为了是敌人们少才坑,我这边将集成的过程记录一下:
- 引入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包,如绿色标记。
- 编写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();
}
}
揭示:留神红色标注的中央
- 在spring-mvc.xml文件中进行过滤器的配置,过滤掉swagger的相干拜访配置:
<mvc:exclude-mapping path="/swagger*/**"/>
<mvc:exclude-mapping path="/v2/**"/>
<mvc:exclude-mapping path="/webjars/**"/>
- 服务配置项
1. <span style="color: #ff0000;">@Api("区域服务")</span>
2. @RestController
3. @RequestMapping(value = "/rest/area")
4. public class AreaService {
6. @Autowired
7. private AreaService areaService;
9. <span style="color: #ff0000;">@ApiOperation(value = "区域列表", httpMethod = "GET", notes = "区域列表")</span>
10. @IsLogin
11. @ResponseBody
12. @RequestMapping(value = "treeData", method = RequestMethod.GET)
13. public List<Map<String, Object>> treeData(
14. <span style="color: #ff0000;">@ApiParam(required = true, value = "区域ID")</span> @RequestParam(required=false) String extId, HttpServletResponse response) {
15. List<Map<String, Object>> mapList = Lists.newArrayList();
16. List<Area> list = areaService.findAll();
17. for (int i=0; i<list.size(); i++){
18. Area e = list.get(i);
19. if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
20. Map<String, Object> map = Maps.newHashMap();
21. map.put("id", e.getId());
22. map.put("pId", e.getParentId());
23. map.put("name", e.getName());
24. mapList.add(map);
25. }
26. }
27. return mapList;
28. }
29. }
- 启动我的项目,查看后果:
到此结束!!(企业架构源码能够加求球:叁五三陆二肆柒二伍玖)
发表回复