为何用swagger2?swagger1和2试用对比php
在springMVC项目中引入swagger2html
springfox与swagger的关系java
对第2步配置的说明web
1.为何用swagger2?swagger1和2试用对比spring
试用过swagger-springmvc和springfox-swagger2后,为了能限定暴露哪些接口,选用swagger2。json
2.在springMVC项目中引入swagger2api
1.配置依赖关系tomcat
pom.xmlmvc
<!-- swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>
注:没必要引入jackson-core, jackson-databind,会形成tomcat启动报错:app
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is com.google.common.util.concurrent.ExecutionError: com.google.common.util.concurrent.ExecutionError: java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonProperty$Access
缘由:pom有最近多余添加的的依赖,删除便可。
2.配置Swagger2Congig类
@Configuration @EnableSwagger2 @EnableWebMvc @ComponentScan(basePackages = {"cn.com.sinosure.appApi.controller"}) public class Swagger2Config extends WebMvcConfigurerAdapter { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() // 选择那些路径和api会生成document .paths(Predicates.not(PathSelectors.regex("/error.*"))) .apis(RequestHandlerSelectors.basePackage("cn.com.xx.packageName")) // 根据包名选择 //.apis(RequestHandlerSelectors.any()) // 对全部api进行监控 .paths(PathSelectors.any()) // 对全部路径进行监控 .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档") .description("springfox-swagger2") .termsOfServiceUrl("") .version("1.0") .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //enabling swagger-ui part for visual documentation registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
注:自定义的swagger2配置类应在<context:component-scan>配置的base-package路径下,不然会形成访问 http://localhost:8080/<projectName>/swagger-ui.html 除swagger图标正常显示外,其余空白。
3.写一个Test类(试用)
@Controller @RequestMapping("/test") public class Test { @ApiOperation(value = "一个测试API", notes = "第一个测试api") @ResponseBody @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "hello"; } }
4.http://localhost:8080/swagger-ui.html
3.springfox与swagger的关系
Swagger API开发框架,基于OAS(OpenAPI Specification,一个API规范,规范RESTful服务开发过程;描述API接口,采用YAML格式/json格式)。
初代:基于Spring的组件:swagger-springmvc
后发展为springfox,其中的一个组件springfox-swagger2,能够自动生成符合OAS规范的json文件;另外一个组件springfox-swagger-ui负责解析此json文件,以友好的界面形式展示。
4.对第2步配置的说明
任务拆解
选择哪些路径和api会暴露出来;
将API描述信息封装到apiInfo类中,做为参数传给Docket;
全局覆写HTTP方法的返回信息。
验收
http://localhost:8080/<projectName>/v2/api-docs
在swagger-springmvc组件中,须要单独引用swagger-ui的页面显示包(把dist目录下的内容拷到项目中WEB-INF目录下)。
而使用升级后的springfox-swagger2,只须要添加对springfox-swagger-ui的依赖。
http://localhost:8080/<projextName>/swagger-ui.html
5.参考:
Setting Up Swagger 2 with a Spring REST API
springfox-swagger原理解析与使用过程当中遇到的坑
swagger-springmvc:Restful形式接口文档生成之Swagger与SpringMVC整合手记