整合之后,页面:
0.springboot版本1.4.1(compile group: 'org.springframework.boot', name: 'spring-boot', version: '1.4.1.RELEASE')
对应swagger版本:2.6.1
1. 需要的jar
guava-18.0.jar
jackson-annotations-2.8.0.jar
jackson-databind-2.6.6.jar
springfox-core-2.6.1.jar
springfox-schema-2.6.1.jar
springfox-spi-2.6.1.jar
springfox-spring-web-2.6.1.jar
springfox-swagger2-2.6.1.jar
springfox-swagger-common-2.6.1.jar
springfox-swagger-ui-2.6.1.jar
spring-plugin-core-1.2.0.RELEASE.jar
spring-plugin-metadata-1.2.0.RELEASE.jar
swagger-annotations-1.5.10.jar
swagger-models-1.5.10.jar
用maven引入的话,只需要在pom文件添加以下两个依赖即可:
<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>
2. 创建SwaggerConfig.java
package gwxm.sffw;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//import io.swagger.models.Contact;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger API文档url为: http://localhost:8089/gwxmsffw/swagger-ui.html
* @author fuhm
*
*/
@ComponentScan("gwxm.sffw.result.controller") //需要扫描的api位置
@Configuration //启动时加载类
@EnableSwagger2 //启用Swagger API文档
public class SwaggerConfig {
/**
* swagger2的配置文件,这里可以配置swagger2的一些基本的内容,
* 比如扫描的包等等
* @return docket
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("gwxm.sffw.result")) //为当前包路径 ,扫描controller包
// .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) // 含有RestController的注解
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // 含有api的注解
.paths(PathSelectors.any()) // 所有controller
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("gwxmsffw 测试使用 Swagger2 构建RESTful API")
.description("gwxmsffw API 1.0 操作文档")
.termsOfServiceUrl("https://www.cnblogs.com/wadmwz/") //服务条款网址
.version("1.0")
// .contact( "gwxmsffw contact" )
.build();
}
}
3. 启动类添加注解 : @EnableSwagger2
若是springboot项目,此时启动项目。访问 http://localhost:8094/swagger-ui.html#/ 就可以了(8094是自己项目的端口号)。若404,则自定义资源目录,即第四步:
4. 创建WebMvcConfig.java
package gwxm.sffw;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
5. 注意拦截器不要把以下请求拦截!
/swagger-ui.html
/webjars/*
/v2/api-docs
/swagger-resources/*
6.访问路径