一. build.gradle引入包
https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui(该包里面本身含有显示页面)
或
https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui
二. 创建SwaggerConfig配置类
@Configuration
@EnableSwagger2
//@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("swagger")
.description("swagger API")
.termsOfServiceUrl("http://xxx.com")
.license("")
.licenseUrl("")
.version("1.0.0")
.build();
}
}
三. WebConfig类添加静态资源访问路径
springfox-swagger-ui或swagger-bootstrap-ui包里面本身包含ui访问页面,所以不需要添加额外的ui包
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
//swagger-ui页面显示
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
//doc.html页面显示
registry.addResourceHandler("/doc.html")
.addResourceLocations("classpath:/META-INF/resources/doc.html");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
四. controller类插入swagger注解
五. 访问
页面访问:http://localhost:8080/swagger-ui.html#,或http://localhost:8080/doc.html
JSON数据访问:http://localhost:8080/v2/api-docs
目录
目录