1. 导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2. 新建Swagger配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//对外暴露服务的包,以controller的方式暴露,所以就是controller的包.
.apis(RequestHandlerSelectors.basePackage("cn.test"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("系统中心api")
.description("系统中心服务接口文档说明")
.contact(new Contact("test", "", "admin@test.cn"))
.version("1.0")
.build();
}
}
3. 登录web页面访问
127.0.0.1/swagger-ui.html
4. Spring拦截器放行Swagger资源
.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")

本文档介绍了如何在Spring Boot应用中集成Springfox,通过 Swagger2 和 Swagger-UI 来创建RESTful API的交互式文档。首先,引入了Swagger的相关依赖。接着,创建Swagger配置类`SwaggerConfig`,配置API的基本信息并暴露指定包下的所有Controller。最后,通过设置Spring拦截器放行Swagger的资源,可以在浏览器中访问`swagger-ui.html`来查看和测试API。
723

被折叠的 条评论
为什么被折叠?



