- 添加maven依赖
io.springfox springfox-swagger22.9.2 io.springfox springfox-swagger-ui2.9.2 com.github.xiaoymin knife4j-spring-boot-starter2.0.2
- 添加SwaggerConfig配置类
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//下面的路径根据自己项目的实际api路径调整
.apis(RequestHandlerSelectors.basePackage("com.xxx.server.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("swagger api document").description("Api文档").termsOfServiceUrl("http://www.xxx.com").version("1.0").build();
}
}
-
配置swagger静态资源映射
@Component public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //swagger UI registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/meta-INF/resources/"); //增强版UI registry.addResourceHandler("doc.html").addResourceLocations("classpath:/meta-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/meta-INF/resources/webjars/"); } } - 访问地址
- swagger UI http://localhost:9200/swagger-ui.html#/
- 增强版UI http://localhost:9200/doc.html



