首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger核v3在java中的实现

Swagger核v3在java中的实现
EN

Stack Overflow用户
提问于 2019-06-05 10:37:54
回答 2查看 3.8K关注 0票数 1

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

我使用了Swagger核心v3符号,所以我的类如下所示:

代码语言:javascript
复制
@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 ...
    }

}

我创建了一个配置类:

代码语言:javascript
复制
@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();
    }
}

ANd我想让Swagger的UI获得文档,但是当我输入:./swagger-ui.html时

我得到:

无法呈现此定义,所提供的定义没有指定有效的版本字段。 请指定有效的Swagger或OpenAPI版本字段。支持的版本字段是swagger:"2.0“和那些匹配openapi: 3.0.n的字段(例如openapi: 3.0.0)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-05 10:45:31

尝试用WebMvcConfigurationSupport扩展您的SwaggerConfig类,并使用如下实现覆盖它的方法addResourceHandlers

代码语言:javascript
复制
@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/");
}
票数 1
EN

Stack Overflow用户

发布于 2019-06-05 12:11:00

在类的顶部,您是否可以尝试删除这个注释,因为SwaggerConfig已经包含了文档信息。

代码语言:javascript
复制
  @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")
            ))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56458833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档