端点按资源招摇注释分组?招摇、注释、资源

2023-09-08 09:23:06 作者:南巷初晴

我正在使用 Spring 进行 REST API 开发.而且我有一些 API,其中有很多端点.当我打开 swagger ui 时,它看起来已经打包好了.

我刚刚阅读了

根据句意和汉语注释.写出单词的正确形式.1.Tourists. both from and . enjoy the delicious food in Wuxi.2.He didn t say anything to her. and

********** 解决方案 2:(使用标签) **********

你不需要定义多个Docket bean,一个就够了.

@Bean公共案卷 api1() {//这里的标签是可选的,它只是在 UI 中添加一个描述//默认描述是类名,所以如果你使用相同的标签//`@Api` 在不同的类上,它将选择一个类名作为//描述,所以最好为它们定义你自己的描述返回新案卷(DocumentationType.SWAGGER_2).tags(new Tag("users", "users related"),新标签(产品",产品相关")).选择().apis(RequestHandlerSelectors.basePackage("com.github")).建造();}

之后,您只需使用 @Api (在类级别,默认为所有方法)或 @ApiOperation (在方法级别,将覆盖)注释您的 api 方法类级别的价值).

@RestController@RequestMapping("/api/products")@Api(标签 = "产品")公共类产品控制器 {@ApiOperation(值 = "", 标签 = "产品")@RequestMapping(方法 = RequestMethod.POST)@ResponseStatus(HttpStatus.CREATED)公共产品 createProduct(@RequestBody 产品产品) {退货;}}

@ApiOperation(或@Api)中的标签也可以跨控制器工作,即使用给定标签标记的不同控制器类(或控制器本身)中的方法将被组合在一起.

I'm using Spring for my REST API development. And I have some API where there are lots of endpoints. When I open up swagger ui, it looks to packed.

I just read this article and saw that we can group endpoints based on resource level.

I just want to know how that can be achieved with swagger annotations with Spring. I appreciate if someone can describe with an example.

And also I just wonder whether we can regroup (higher level grouping) the groups we have deduces in above way?

解决方案

********** SOLUTION 1: (using groups) **********

Just define multiple Docket bean for each group, and u will get logical grouping as per your need.

@Bean
public Docket api1() {

return new Docket(DocumentationType.SWAGGER_2)
    .groupName("users")
    .select()
    .paths(PathSelectors.ant("/api/users/**"))
    .build();
}

@Bean
public Docket api2() {

return new Docket(DocumentationType.SWAGGER_2)
    .groupName("products")
    .select()
    .paths(PathSelectors.ant("/api/products/**"))
    .build();
}

Now you will get two groups in your swagger ui like below.

********** SOLUTION 2: (using tags) **********

You don't need to define multiple Docket bean just one is enough.

@Bean
public Docket api1() {

// here tags is optional, it just adds a description in the UI
// by default description is class name, so if you use same tag using 
// `@Api` on different classes it will pick one of the class name as 
// description, so better define your own description for them
return new Docket(DocumentationType.SWAGGER_2)
    .tags(new Tag("users", "users related"), 
          new Tag("products", "products related"))
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.github"))
    .build();
}

After that you just need to annotate your api methods with @Api (at class level, default for all methods) or @ApiOperation (at method level, will override value at class level).

@RestController
@RequestMapping("/api/products")
@Api(tags = "products")
public class ProductController {

    @ApiOperation(value = "", tags = "products")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Product createProduct(@RequestBody Product product) {

        return product;
    }
}

Tags in @ApiOperation (or in @Api) will work across controller as well, i.e. method in different controller classes (or controller itself) tagged with a given tag will be grouped together.

 
精彩推荐
图片推荐