java 中 Swagger 核心 v3 的实现

Implementation of Swagger core v3 in java

我正在写一些 API,我想在编写代码的同时集成文档,所以我发现 Swagger 是一个很好的方法。

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

@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 ...
    }

}

然后我创建一个配置 class:

@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 来获取文档,但是当我输入: .../招摇-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 class 并重写其名为 addResourceHandlers[= 的方法16=] 像这样实现:

@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/");
}

在 class 之上,您能否尝试删除此注释,因为 SwaggerConfig 已经包含 Docket 信息。

  @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")
            ))