我习惯于在我的REST控制器上添加注释,以供Swagger-UI使用。但是,我希望将Swagger-UI指向描述我的REST控制器的YAML文件。下面是我想做的一个例子。 (Springfox / Swagger2)
DemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
SwaggerConfig.java
注意,我试图告诉Swagger基于YAML文件而不是REST控制器构建Docket。
import com.google.common.base.Predicate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).useDefaultResponseMessages(false)
.select()
.paths(paths())
.build();
}
private Predicate<String> paths() {
return regex("/swagger.yml");
}
}
swagger.yml
这是一个示例YAML文件,描述了我的REST控制器的外观,这就是我希望Swagger-UI使用的外观。
swagger: "2.0"
paths:
/animals:
post:
summary: Creates an animal.
responses:
'201':
description: Created.
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.8.0'
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.8.0'
}
如果使用YAML文件无法实现,但是可以使用其他格式(例如JSON),请随时使用该解决方案进行回答。
您需要通过SwaggerResourceProvider
插入YAML定义。
如果您需要保留基于注释的大招:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Primary
@Bean
public SwaggerResourcesProvider swaggerResourcesProvider(InMemorySwaggerResourcesProvider defaultResourcesProvider) {
return () -> {
SwaggerResource wsResource = new SwaggerResource();
wsResource.setName("Documentation");
wsResource.setSwaggerVersion("2.0");
wsResource.setLocation("/swagger.yaml");
List<SwaggerResource> resources = new ArrayList<>(defaultResourcesProvider.get());
resources.add(wsResource);
return resources;
};
}
}
如果您只想使用基于YAML的招摇工具:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Primary
@Bean
public SwaggerResourcesProvider swaggerResourcesProvider() {
return () -> {
SwaggerResource wsResource = new SwaggerResource();
wsResource.setName("Documentation");
wsResource.setSwaggerVersion("2.0");
wsResource.setLocation("/swagger.yaml");
List<SwaggerResource> resources = List.of(wsResource);
return resources;
};
}
}
您需要将您的YAML文件放入src/main/resource/static