文档 : http://springfox.github.io/springfox/docs/current/

pom.xml 加入依赖

1
2
3
4
5
6
7
8
9
10
<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>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

@Configuration
@EnableSwagger2
public class Swagger2Config {

@Bean
public Docket docket () {
return new Docket( DocumentationType.SWAGGER_2 )
.apiInfo( apiInfo() )
.select()
.apis( RequestHandlerSelectors.basePackage( "com.aidijing" ) )
.paths( PathSelectors.any() )
.build();
}

private ApiInfo apiInfo () {
return new ApiInfoBuilder()
.title( "RESTful API" )
.description( "api接口文档" )
.contact( new Contact( "披荆斩棘", "http://www.pijingzhanji.com/", "yujunhao_8831@yahoo.com" ) )
.version( "1.0" )
.build();
}

}

这样就配好了,打开浏览器访问 http://localhost:8080/swagger-ui.html

注意: Spring boot 是默认支持下面的静态映射的,如果是Spring 项目,则需要对 swagger 的静态文件放行,配置文件中加入静态资源的访问

1
2
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>