Java 如何让Swagger UI使用YAML/JSON,而不必在REST控制器上添加注释?

Java 如何让Swagger UI使用YAML/JSON,而不必在REST控制器上添加注释?,java,yaml,swagger,swagger-ui,springfox,Java,Yaml,Swagger,Swagger Ui,Springfox,我习惯于在我的REST控制器上添加注释以供Swagger UI使用。然而,我更愿意将Swagger UI指向一个描述我的REST控制器的YAML文件。下面是我想做的一个例子。(斯普林福克斯/大摇大摆2) DemoApplication.java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @Sprin

我习惯于在我的REST控制器上添加注释以供Swagger UI使用。然而,我更愿意将Swagger UI指向一个描述我的REST控制器的YAML文件。下面是我想做的一个例子。(斯普林福克斯/大摇大摆2)

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

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);
    }
}
注意,我试图告诉Swagger基于YAML文件而不是REST控制器构建一个摘要

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");
    }
}
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),请使用该解决方案进行回答。

您需要通过
Swagger ResourceProvider
插入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;
        };
    }
}
@配置
@使能招摇过市2
公共类招摇过市配置{
@豆子
公开摘要api(){
返回新摘要(DocumentationType.SWAGGER_2)
.选择()
.api(RequestHandlerSelectors.any())
.path(路径选择器.any())
.build();
}
@初级的
@豆子
公共炫耀资源提供者炫耀资源提供者(InMemorySwaggerResourcesProviderDefaultResourcesProvider){
返回()->{
SwaggerResource wsResource=新建SwaggerResource();
wsResource.setName(“文档”);
wsResource.setSwaggerVersion(“2.0”);
wsResource.setLocation(“/swagger.yaml”);
List resources=new ArrayList(defaultResourcesProvider.get());
添加(wsResource);
归还资源;
};
}
}
如果您想使用基于YAML的just swagger:

@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;
        };
    }
}
@配置
@使能招摇过市2
公共类招摇过市配置{
@初级的
@豆子
公共炫耀资源提供者炫耀资源提供者(){
返回()->{
SwaggerResource wsResource=新建SwaggerResource();
wsResource.setName(“文档”);
wsResource.setSwaggerVersion(“2.0”);
wsResource.setLocation(“/swagger.yaml”);
List resources=List.of(wsResource);
归还资源;
};
}
}

你需要把你的YAML文件放到
src/main/resource/static

在Springdoc/OpenAPI中有我可以这样做的吗?在Springdoc/OpenAPI中有我可以这样做的吗?@user545871它可以将版本设置为3.0,它会自动识别(打开api 3)