所以我试图创建一个别名

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiResponses {
    ApiResponse[] value();
}


我尝试过的

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface targetTest {
    @AliasFor(annotation = ApiResponses.class, attribute="value")
    ApiResponse[] value()
    default {
        @ApiResponse(code = 200, message = "test1"),
        @ApiResponse(code = 400, message = "test2"),
        @ApiResponse(code = 404, message = "test3"),
        @ApiResponse(code = 500, message = "test4")
    };
}


当我使用时,这不会覆盖ApiResponses批注

@targetTest
doSomething()


有什么建议么?

最佳答案

您需要在@ApiResponses上添加TargetTest

@Target({ANNOTATION_TYPE, METHOD})
@Retention(RUNTIME)
public @interface ApiResponses {
    ApiResponse[] value() default {};
}

@Target(METHOD)
@Retention(RUNTIME)
@ApiResponses
public @interface TargetTest {
    @AliasFor(annotation = ApiResponses.class)
    ApiResponse[] value() default {...};
}

09-09 17:44