英文:
Unable to create meta-annotation for @RequestBody in Springboot
问题
我想要创建一个名为 @QueryRequest
的元注解,用于类似下面展示的 Spring 的 @RequestBody
。
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@RequestBody
public @interface QueryRequest {
}
然而,它抛出了一个编译错误,错误信息为:
java: annotation type not applicable to this kind of declaration
当我在互联网上搜索时,它告诉我要验证正确的 @Target
类型。无论如何,正如您已经看到的我的 @Target
和 @Retention
值,它们与 Spring 的 @RequestBody
是相同的,但仍然出现了上述错误。
我已经成功地为 @Target=ElementType.METHOD
或 ElementType.TYPE
类型创建了元注解,但是我无法使上述注解起作用。
有人知道上述元注解实际上有什么问题吗?
英文:
I want to create a meta-annotation, called @QueryRequest
, for Spring's @RequestBody
like shown in below.
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@RequestBody
public @interface QueryRequest {
}
However, it throws a compilation error called,
java: annotation type not applicable to this kind of declaration
When I searched in the internet, it tells me to verify the correct @Target
type. Anyway, as you can already see my @Target
and @Retention
values, they are as same as what Spring's @RequestBody
is, but still above error is thrown.
I have successfully created meta-annotations for @Target=ElementType.METHOD
or ElementType.TYPE
types, but I could not make work above annotation.
Anybody know what is actually wrong with above meta-annotation?
答案1
得分: 2
因为@RequestBody
被注解为@Target(ElementType.PARAMETER)
,所以你只能将这个注解添加在一个参数上。在这里,你试图将该注解应用于另一个注解上。为了实现这一点,@RequestBody
应该被注解为@Target(ElementType.ANNOTATION_TYPE)
或者@Target(ElementType.TYPE)
。
例如,以下代码将不起作用,因为你不能在一个注解上注解QueryRequest
:
@Target(ElementType.PARAMETER)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryRequest {
}
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@QueryRequest
@interface NextQueryRequest
然而,以下代码将起作用,因为你允许将QueryResult
放在一个注解上:
@Target(ElementType.TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryRequest {
}
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@QueryRequest
@interface NextQueryRequest
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryRequest {
}
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@QueryRequest
@interface NextQueryRequest
英文:
Since @RequestBody is annotated @Target(ElementType.PARAMETER)
you can only add this annotation on a parameter. Here you are trying to apply the annotation on an annotation. In order to achieve that @RequestBody
should have been annotated with @Target(ElementType.ANNOTATION_TYPE)
or with @Target(ElementType.TYPE)
.
For example this code will not work because you cannot annotate QueryRequest on an annotation:
@Target(ElementType.PARAMETER)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryRequest {
}
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@QueryRequest
@interface NextQueryRequest
However this will work, because you are allowing QueryResult to be put on an annotation
@Target(ElementType.TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryRequest {
}
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@QueryRequest
@interface NextQueryRequest
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface QueryRequest {
}
@Target(ElementType.ANNOTATION_TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@QueryRequest
@interface NextQueryRequest
答案2
得分: 0
@Daniel已经用一个例子解释了发生这种情况的原因。
另外,任何寻找解决方法的人都应该阅读这个答案,就像@Michiel在上面提到的那样。
https://stackoverflow.com/a/40861154/2148365
英文:
@Daniel has explained why this happens with an example.
Also, anyone who is looking for a workaround should read this answer as @Michiel mentioned above.
https://stackoverflow.com/a/40861154/2148365
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论