2020-08-25更新,新版配置请参考 点击访问 https://www.aiprose.com/blog/127
//gradle
compile group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
//maven
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
这里是更新分割线
最近的一个项目需要使用udp服务外加几个Restful的web接口,因为一直使用SpringBoot技术栈,就默认选择了Springboot+Webflux的模式. 提供给大前端的接口文档还是选用Swagger的方式.
一直使用的springfox-swagger2 2.9.2版本,在集成到webflux之后,整个application不工作了,搜索后有文章说2.9.2支持webflux,不知道说的支持是什么层面,笔者测试是无法使用的。根据外网搜到的内容,对webflux的支持,是从3.0.0-SNAPSHOT开始,并且因为还是snapshot状态,没有进入maven中央仓库,需要添加repository.
maven方式
<repositories>
<repository>
<id>jcenter-snapshots</id>
<name>jcenter</name>
<url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
</repository>
</repositories>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-webflux</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
gradle方式
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
implementation 'io.springfox:springfox-swagger2:3.0.0-SNAPSHOT'
implementation 'io.springfox:springfox-spring-webflux:3.0.0-SNAPSHOT'
implementation 'io.springfox:springfox-swagger-ui:3.0.0-SNAPSHOT'
加完这个依赖后,对swagger的配置,也要做一些修改,核心的部分是@EnableSwagger2
这个注解要换成@EnableSwagger2WebFlux
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfiguration {
//上线后改成false
@Value(value = "${swagger.enabled}")
Boolean swaggerEnabled;
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("军医大")
.description("军医大")
// 作者信息
.contact(new Contact("nelson", "https://www.aiprose.com", "mail_yanpeng@163.com"))
.version("1.0.0")
.build())
//分组名称
.groupName("军医大")
.enable(swaggerEnabled).select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.trgis.drill"))
.paths(PathSelectors.any())
.build()
//下面这个是配置全局token的,不需要的请不要加
.securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));;
return docket;
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("/.*"))
.build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Lists.newArrayList(new SecurityReference("token", authorizationScopes));
}
private ApiKey apiKey() {
return new ApiKey("token", "token", "header");
}
}