Swagger2配置
maven
<!-- swagger2 依赖 接口文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Swagger第三方ui依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// 扫描有哪些包下生成 Swagger文档
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.abin.controller"))
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("云e办接口文档")
.description("云e办接口文档")
.contact(new Contact("crabin" , "http://localhost:8088/doc.html","1528392308@qq.com"))
.version("1.0")
.build();
}
}
导入版本正确可以,加入报错:Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is
可能是因为Springboot版本太高导致的
两种解决方法:
1.编写 WebMvcConfigurer 配置类
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
2.在 application.yaml 中:
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
访问:http://localhost:8088/doc.html
调试:
在createRestApi()方法后面继续 设置权限:
.securityContexts(securityContexts())
.securitySchemes(securitySchemes());
@Configuration
@EnableSwagger2
public class SwaggerConfig {
// 扫描有哪些包下生成 Swagger文档
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.abin.controller"))
.paths(PathSelectors.any())
.build()
.securityContexts(securityContexts())
.securitySchemes(securitySchemes());
}
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("云e办接口文档")
.description("云e办接口文档")
.contact(new Contact("crabin" , "http://localhost:8088/doc.html","1528392308@qq.com"))
.version("1.0")
.build();
}
private List<ApiKey> securitySchemes() {
// 设置请求头
List<ApiKey> result = new ArrayList<>();
ApiKey apiKey = new ApiKey("Authorization", "Authorization", "Header");
result.add(apiKey);
return result;
}
private List<SecurityContext> securityContexts() {
// 设置需要登录认证的路径
List<SecurityContext> result = new ArrayList<>();
result.add(getContextByPath("/hello/.*"));
return result;
}
private SecurityContext getContextByPath(String pathRegex) {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(pathRegex))
.build();
}
private List<SecurityReference> defaultAuth() {
List<SecurityReference> result = new ArrayList<>();
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
result.add(new SecurityReference("Authorization", authorizationScopes));
return result;
}
}