组合注释中的 JAX-RS @Path

JAX-RS @Path in Composed Annotation

这个看起来比较简单。我在搞乱组合注释,我正在尝试执行以下操作:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Path("")
public @interface TestAnnotation {
  @AliasFor(annotation = Path.class, attribute = "value")
  String path();
}

这不起作用。当我这样使用它时:

@Path("")
public class MyResource {
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @TestAnnotation(path = "/things")
  public void postIt(Thing myThing) {
    // Do various things and then return a Response
  }
}

...我在 return 中收到 405。如果我这样做:

// Remove class-level @Path
// @Path("")
public class MyResource {
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @TestAnnotation(path = "/things")
  public void postIt(Thing myThing) {
    // Do various things and then return a Response
  }
}

...我在 return 中收到 404

@Path@Path 具有必需的 value 属性导致此功能无法正常工作,我不知道如何补救.

经过进一步的实验和研究,我想做的事情似乎是不可能的。

follow-up 尝试利用 Jackson 的 @JsonView 并通过我通过 Spring 的 @AliasFor 编写的注释公开它,但这也未能发挥作用。

我花了一些时间思考注释是如何工作的,并考虑了 peeskillet 关于编译与处理的评论,我得出的结论是 Jersey 和 Jackson 必须使用基本上只调用 "Method.isAnnotationPresent()" 的注释处理器相关注释的检测。 Spring 的 @AliasFor 很可能不会编译或编织别名 meta-annotations 到目标方法的 byte-code 中,因此它们不会被处理器找到。

我对这个问题的个人解决方案是完全放弃 JAX-RS 并使用 Spring 的 @RequestMapping,其中 可以 别名一个组合注释,并接受 Jackson 的 @JsonView 根本无法集成到一个组合注释中。

显然,对于大多数人来说,这不是一个理想的解决方案,尤其是那些已经建立了大型系统的人。在这种情况下,组合注释的想法很可能会在 JAX-RS.

之前就被放弃。

因此,如果有人对问题有更深入的了解,或者直接来自 Spring 团队的人想要插话,请随意。