无法在Springboot中为@RequestBody创建元注释

huangapple go评论73阅读模式
英文:

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.METHODElementType.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

huangapple
  • 本文由 发表于 2020年8月15日 22:00:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定