因為公司使用的spring版本太高舞骆,在集成swagger的時候會存在一些問題曹步,而網(wǎng)上的很多實例大多都是版本比較低的,為了是朋友們少才坑魔市,我這邊將集成的過程記錄一下:
1. 引入spring主届、swagger的相關(guān)jar包(springfox-swagger2、springfox-swagger-ui)待德,在pom.xml中配置:
Xml代碼
io.springfox
springfox-swagger2
2.4.0
org.springframework
spring-core
org.springframework
spring-beans
org.springframework
spring-context
org.springframework
spring-context-support
org.springframework
spring-aop
org.springframework
spring-tx
org.springframework
spring-orm
org.springframework
spring-jdbc
org.springframework
spring-web
org.springframework
spring-webmvc
org.springframework
spring-oxm
io.springfox
springfox-swagger-ui
2.4.0
io.springfox
springfox-swagger2
2.4.0
org.springframework
spring-core
org.springframework
spring-beans
org.springframework
spring-context
org.springframework
spring-context-support
org.springframework
spring-aop
org.springframework
spring-tx
org.springframework
spring-orm
org.springframework
spring-jdbc
org.springframework
spring-web
org.springframework
spring-webmvc
org.springframework
spring-oxm
io.springfox
springfox-swagger-ui
2.4.0
提醒: 特別注意君丁,springfox-swagger2在集成的時候,已經(jīng)引入了spring的相關(guān)jar将宪,特別是spring-context绘闷、spring-context-support的版本和項目中使用的版本完全不一致,項目在啟動的時候出現(xiàn)很多包沖突的問題较坛,這邊在引入pom.xml文件的時候過濾掉了spring的相關(guān)jar包印蔗,如綠色標志。
2. 編寫Swagger的配置類:
Java代碼
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"})
publicclass?SwaggerConfig?{
@Bean
public?Docket?createRestApi()?{
returnnew?Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.ml.honghu"))
.paths(PathSelectors.any())
.build();
}
private?ApiInfo?apiInfo()?{
returnnew?ApiInfoBuilder()
.title("接口列表?v1.0")
.description("接口信息")
.termsOfServiceUrl("http://honghu.com")
.contact(new?Contact("","","HongHu"))
.version("1.1.0")
.build();
}
}
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的相關(guān)訪問配置:
Java代碼
4. 服務(wù)配置項
Java代碼
@Api("區(qū)域服務(wù)")
@RestController
@RequestMapping(value?="/rest/area")
publicclass?AreaService??{
@Autowired
private?AreaService?areaService;
@ApiOperation(value?="區(qū)域列表",?httpMethod?="GET",?notes?="區(qū)域列表")
@IsLogin
@ResponseBody
@RequestMapping(value?="treeData",?method?=?RequestMethod.GET)
public?List>?treeData(
@ApiParam(required?=true,?value?="區(qū)域ID")@RequestParam(required=false)?String?extId,?HttpServletResponse?response)?{
List>?mapList?=?Lists.newArrayList();
List?list?=?areaService.findAll();
for?(int?i=0;?i
Area?e?=?list.get(i);
if?(StringUtils.isBlank(extId)?||?(extId!=null?&&?!extId.equals(e.getId())?&&?e.getParentIds().indexOf(","+extId+",")==-1)){
Map?map?=?Maps.newHashMap();
map.put("id",?e.getId());
map.put("pId",?e.getParentId());
map.put("name",?e.getName());
mapList.add(map);
}
}
return?mapList;
}
}
@Api("區(qū)域服務(wù)")
@RestController
@RequestMapping(value = "/rest/area")
public class AreaService? {
@Autowired
private AreaService areaService;
@ApiOperation(value = "區(qū)域列表", httpMethod = "GET", notes = "區(qū)域列表")
@IsLogin
@ResponseBody
@RequestMapping(value = "treeData", method = RequestMethod.GET)
public List> treeData(
@ApiParam(required = true, value = "區(qū)域ID")? @RequestParam(required=false) String extId, HttpServletResponse response) {
List> mapList = Lists.newArrayList();
List list = areaService.findAll();
for (int i=0; i
Area e = list.get(i);
if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
Map map = Maps.newHashMap();
map.put("id", e.getId());
map.put("pId", e.getParentId());
map.put("name", e.getName());
mapList.add(map);
}
}
return mapList;
}
}
4. 啟動項目华嘹,查看結(jié)果:
愿意了解框架技術(shù)或者源碼的朋友直接求求交流分享技術(shù):3133806896
分布式的一些解決方案,有愿意了解的朋友可以找我們團隊探討