有没有一种方法可以为不同的方法共享醒目注解?

时间:2019-04-26 14:17:05

标签: java rest spring-boot

我有不同的RestAPI方法对应于端点,并且我想共享它们的@ApiResponses以避免代码重复。我基本上对8种不同的方法进行了以下注释,并且理想情况下,我只希望对其进行一次定义。

@ApiResponses(value = {@ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 400, message = "Bad Request", response = ResourceErrorModel.class),
            @ApiResponse(code = 401, message = "Unauthorized", response = ResourceErrorModel.class),
            @ApiResponse(code = 403, message = "Forbidden", response = ResourceErrorModel.class),
            @ApiResponse(code = 404, message = "Not Found", response = ResourceErrorModel.class),
            @ApiResponse(code = 409, message = "Conflict", response = ResourceErrorModel.class),
            @ApiResponse(code = 422, message = "Unprocessable Entity", response = ResourceErrorModel.class),
            @ApiResponse(code = 424, message = "Failed Dependency", response = ResourceErrorModel.class)})

3 个答案:

答案 0 :(得分:2)

您可以尝试创建自己的注释并将其添加到您的方法中:

@Target(value=METHOD)
@Retention(value=RUNTIME)
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 400, message = "Bad Request", response = ResourceErrorModel.class),
            @ApiResponse(code = 401, message = "Unauthorized", response = ResourceErrorModel.class),
            @ApiResponse(code = 403, message = "Forbidden", response = ResourceErrorModel.class),
            @ApiResponse(code = 404, message = "Not Found", response = ResourceErrorModel.class),
            @ApiResponse(code = 409, message = "Conflict", response = ResourceErrorModel.class),
            @ApiResponse(code = 422, message = "Unprocessable Entity", response = ResourceErrorModel.class),
            @ApiResponse(code = 424, message = "Failed Dependency", response = ResourceErrorModel.class)})
public @interface SomeCustomAnnotation {}

答案 1 :(得分:1)

您可以创建自己的注释,并用@ApiResponses进行注释:

@Retention(RUNTIME)
@Target({METHOD, TYPE})
@ApiResponses({
        @ApiResponse(code = 200, message = "OK"),
        @ApiResponse(code = 400, message = "Bad Request", response = ResourceErrorModel.class),
        @ApiResponse(code = 401, message = "Unauthorized", response = ResourceErrorModel.class),
        @ApiResponse(code = 403, message = "Forbidden", response = ResourceErrorModel.class),
        @ApiResponse(code = 404, message = "Not Found", response = ResourceErrorModel.class),
        @ApiResponse(code = 409, message = "Conflict", response = ResourceErrorModel.class),
        @ApiResponse(code = 422, message = "Unprocessable Entity", response = ResourceErrorModel.class),
        @ApiResponse(code = 424, message = "Failed Dependency", response = ResourceErrorModel.class)})
public @interface DefaultApiResponses {

}

然后只需注释您的控制器方法:

@GetMapping("/foo")
@DefaultApiResponses
public Response<Foo> getFoo() {
    ...
}

答案 2 :(得分:1)

创建CustomAPIResponse的自定义界面,如下所示:

@Target({METHOD, TYPE})
@ApiResponses(value = {
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 400, message = "Bad Request", response = ResourceErrorModel.class),
            @ApiResponse(code = 401, message = "Unauthorized", response = ResourceErrorModel.class),
            @ApiResponse(code = 403, message = "Forbidden", response = ResourceErrorModel.class),
            @ApiResponse(code = 404, message = "Not Found", response = ResourceErrorModel.class),
            @ApiResponse(code = 409, message = "Conflict", response = ResourceErrorModel.class),
            @ApiResponse(code = 422, message = "Unprocessable Entity", response = ResourceErrorModel.class),
            @ApiResponse(code = 424, message = "Failed Dependency", response = ResourceErrorModel.class)})
public @interface CustomAPIResponse{}

然后,通过REST API的每个控制器方法,只需使用@CustomAPIResponse对该函数进行注释。这将仅添加CustomAPIResponse类中提到的所有注释。

您可以创建多个此类@interface类,并且单个函数可以使用上面创建的多个自定义注释进行注释。