不建议使用Api注释的描述 [英] Api annotation's description is deprecated

查看:150
本文介绍了不建议使用Api注释的描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swagger中,解决方案

我为Spring Boot应用程序找到了两种解决方案:

1. Swagger 2 基于:

首先,使用 tags 方法在 Docket bean中指定标签定义:

 <代码> @Configuration@ EnableSwagger2公共类Swagger2Config {public static final String TAG_1 ="tag1";@豆角,扁豆Public Docket productApi(){返回新的Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("my.package")).build().tags(新标签(TAG_1,标签1描述".))//其他标签在这里....apiInfo(apiInfo());}私人ApiInfo apiInfo(){返回新的ApiInfoBuilder().title(我的API").version("1.0.0&").build();}} 

然后,在 RestController 中,添加带有一个(或多个)标记的 @Api 批注:

  @Api(tags = {SwaggerConfig.TAG_1})@RestController@RequestMapping("tag1-domain")公共类Tag1RestController {...} 

2.基于Swagger 3 (OpenAPI):

类似地,使用 addTagsItem 方法在 OpenAPI bean中指定标签定义:

 <代码> @Configuration公共类OpenApiConfig {public static final String TAG_1 ="tag1";@豆角,扁豆公共OpenAPI customOpenAPI(){最终信息信息=新信息().title(我的API").description(我的API描述".).version("1.0.0&");返回新的OpenAPI().components(new Components()).addTagsItem(createTag(TAG_1,标签1描述".))//其他标签在这里....info(info);}私人标记createTag(字符串名称,字符串描述){最终Tag标签= new Tag();tag.setName(name);tag.setDescription(description);返回标签;}} 

最后,在 RestController 中,只需添加 @Tag 批注:

  @Tag(name = OpenApiConfig.TAG_1)@RestController@RequestMapping("tag1-domain")公共类Tag1RestController {...} 

In Swagger, the @Api annotation's description element is deprecated.

Deprecated. Not used in 1.5.X, kept for legacy support.

Is there a newer way of providing the description?

解决方案

I found two solutions for Spring Boot application:

1. Swagger 2 based:

Firstly, use the tags method for specify the tags definitions in your Docket bean:

@Configuration
@EnableSwagger2
public class Swagger2Config {
    
    public static final String TAG_1 = "tag1";

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2).select()
                .apis(RequestHandlerSelectors.basePackage("my.package")).build()
                .tags(new Tag(TAG_1, "Tag 1 description."))
                // Other tags here...
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("My API").version("1.0.0").build();
    }
}

After, in RestController just add the @Api annotation with one (or more) of the your tags:

@Api(tags = { SwaggerConfig.TAG_1 })
@RestController
@RequestMapping("tag1-domain")
public class Tag1RestController { ... }

2. Swagger 3 based (OpenAPI):

Similarly, use the addTagsItem method for specify the tags definitions in your OpenAPI bean:

@Configuration
public class OpenApiConfig {

    public static final String TAG_1 = "tag1";

    @Bean
    public OpenAPI customOpenAPI() {
        final Info info = new Info()
                .title("My API")
                .description("My API description.")
                .version("1.0.0");

        return new OpenAPI().components(new Components())
                .addTagsItem(createTag(TAG_1, "Tag 1 description."))
                // Other tags here...
                .info(info);
    }

    private Tag createTag(String name, String description) {
        final Tag tag = new Tag();
        tag.setName(name);
        tag.setDescription(description);
        return tag;
    }

}

Finally, in RestController just add the @Tag annotation:

@Tag(name = OpenApiConfig.TAG_1)
@RestController
@RequestMapping("tag1-domain")
public class Tag1RestController { ... }

这篇关于不建议使用Api注释的描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆