一直以来做对外的接口文档都比较原始,基本上都是手写的文档传来传去,最近发现了一个新玩具,可以在接口上省去不少麻烦。
swagger是一款方便展示的API文档框架。它可以将接口的类型最全面的展示给对方开发人员,避免了手写文档的片面和误差行为。
swagger目前有两种swagger和swagger2两种,1比较麻烦,所以不考虑使用。本文主要记录我用swagger2做对外接口的两种方式,方面后面查阅。
一、使用传统的springmvc整合swagger2
1、maven依赖
com.fasterxml.jackson.core
jackson-core
2.8.0
com.fasterxml.jackson.core
jackson-databind
2.6.3
com.fasterxml.jackson.core
jackson-annotations
2.6.3
io.springfox
springfox-swagger2
2.4.0
io.springfox
springfox-swagger-ui
2.4.0
2、spring-mvc.xml 中添加映射静态的配置(其实我项目中把这个去掉也可以,不知道什么情况):
注意:基本的springmvc配置我就不贴了,需要注意的是,如果你看到swagger-ui.html 界面出来,但却一片空白,请检查下你web.xml中拦截器的配置,一定要springmvc先拦截到,然后界面才会显示的。
3、然后是swagger2的配置类:
@Configuration
@EnableSwagger2public class SwaggerConfig extendsWebMvcConfigurationSupport {
@BeanpublicDocket createRestApi() {return newDocket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("net.laoyeyey.yyblog"))
.paths(PathSelectors.any())
.build();
}privateApiInfo apiInfo() {return newApiInfoBuilder()
.title("yyblog项目 RESTful APIs")
.description("yyblog项目api接口文档")
.version("1.0")
.build();
}
}
注意:paths如果在生产情况下可以调整为PathSelectors.none(),即不显示所有接口信息;
4、接口信息配置
即在SpringMvc的Controller中配置相关的接口信息
@Controller
@RequestMapping(value= "aitou")
@Api(description= "测试服务-账户信息查询")public classDailyOperationDataController {
Logger logger= Logger.getLogger(DailyOperationDataController.class);
@AutowiredprivateDail