简体   繁体   中英

JAX-RS @Path in Composed Annotation

This one seems relatively straightforward. I'm messing around with composed annotations, and I'm trying to do the following:

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

This does not work. When I use it like so:

@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
  }
}

...I receive a 405 in return. If I do this:

// 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
  }
}

...I receive a 404 in return.

There is just something about @Path or the fact that @Path has a required value attribute that results in this just not functioning, and I have no idea how to remedy it.

After further experimentation and research, it would appear that what I am trying to do is literally not possible.

A follow-up attempt was made to utilize Jackson's @JsonView and expose it through my composed annotation via Spring's @AliasFor , and this also failed to function.

I spent some time thinking about how annotations work and considering peeskillet's comments about compilation vs. processing, and I have come to the conclusion that both Jersey and Jackson must use annotation processors that basically just call "Method.isAnnotationPresent()" for detection of relevant annotations. Spring's @AliasFor most likely does not compile nor weave the aliased meta-annotations into the byte-code of the target methods, and thus they are not found by the processors.

My personal solution to this problem was to drop JAX-RS entirely and use Spring's @RequestMapping , which can be aliased in a composed annotation, and to just accept that Jackson's @JsonView simply can't be integrated into a composed annotation.

Obviously, this is not an ideal solution for most people, especially those with large, already established systems. In such situations, it is more likely that the idea of composed annotations will be abandoned long before JAX-RS.

So if anyone has some more insight into the problem, or someone directly from the Spring team wants to chime in, feel free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM