java - Swagger core v3的java实现

标签 java swagger swagger-3.0

我正在编写一些 API,并且希望在编写代码时集成文档,因此我发现 Swagger 是实现此目的的好方法。

我使用了 Swagger core v3 符号,所以我的类类似于:

@RestController

@RequestMapping("/api/v1/bundles")

@OpenAPIDefinition(

        info = @Info(
                title = "Lifecycle Management RESTful API.",
                version = "1",
                description = "TODO",
                license = @License(name = "Apache 2.0", url = "xxx"),
                contact = @Contact(url = "xxx", name = "xx", email = "xxx@xxx.fr")
        ))

public class RestBundle {

    @GetMapping(value = "/{nodeId}",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    @Operation(summary = "Get all bundles",
            description = "Get all available bundles status from a specific node")
    public Something(..)  {
        //Something ...
    }

}

我创建了一个配置类:

@Configuration
@EnableSwagger2

public class SwaggerConfig {


    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }


    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("ANF Orchestrator")
                .description("REST API for ANF Orchestrator")
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .termsOfServiceUrl("")
                .version("1.0.0")
                .contact(new Contact("Amine","xxx", "aalaouie@laas.fr"))
                .build();
    }
}

我想启用 Swagger 的 UI 来获取文档,但是当我输入: .../swagger-ui.html

我得到:

Unable to render this definition The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n(for example, openapi: 3.0.0).

最佳答案

尝试使用 WebMvcConfigurationSupport 扩展您的 SwaggerConfig 类,并使用如下实现重写其名为 addResourceHandlers 的方法:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

关于java - Swagger core v3的java实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56458833/

相关文章:

Java6、Guava、泛型、类型推断

java - 将 Java 7 升级到 Java 8 - Java 8 字符串开关是否已弃用?

servicestack - Swagger with ServiceStack 不会在 POST 上将元素发送到服务器

c# - 无效的 Swagger 安全定义

javascript - 获取引用页面Servlet的URL

Swagger 从 1.2 到 2.0 规范的迁移

spring-boot - openapi 3.0 中所有 API 的强制 header

swagger - 如何在 Spring Boot 中禁用 swagger 3 配置

spring-boot - 如何使用 springdoc 在 swagger openapi 规范 3.0 的 @RequestBody 中创建多个模式?

java - Rabbit mq错误: Getting Exception in thread "main" java. io.IOException由: com.rabbitmq.client.ShutdownSignalException引起