为了账号安全,请及时绑定邮箱和手机立即绑定

java中Swagger core v3的实现

java中Swagger core v3的实现

摇曳的蔷薇 2023-01-05 17:24:40
我正在编写一些 API,并且我想在编写代码时集成文档,因此我发现 Swagger 是实现此目的的好方法。我使用了 Swagger 核心 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@EnableSwagger2public 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我得到:无法呈现此定义提供的定义未指定有效的版本字段。请指明有效的 Swagger 或 OpenAPI 版本字段。支持的版本字段有 swagger: "2.0" 和匹配 openapi: 3.0.n 的字段(例如 openapi: 3.0.0)。
查看完整描述

2 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

尝试使用 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/");

}


查看完整回答
反对 回复 2023-01-05
?
翻阅古今

TA贡献1780条经验 获得超5个赞

在课堂之上,您能否尝试删除此注释,因为 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")

            ))


查看完整回答
反对 回复 2023-01-05
  • 2 回答
  • 0 关注
  • 239 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信