1

To prevent an XY Problem I am gonna state my real issue first but my question focuses on my solution for that problem.

I am using Quarkus to build a controller which contains this method:

    @GET
    @Path("image/{user}")
    @Produces("image/png")
    @PermitAll
    public Response getImageUser(@PathParam("user") String user)

For some implementation reasons out of my control this method needs to handle about a dozen different possible @PathParam, so I'd like to do something like this;

    @Path(value = {"image/{user}", "image/{user}/{foo}", "image/{user}/{bar}"})

which is not possible in Quarkus as far as I know. For this purpose I would like to write an Annotation "wrapper" for the @Path Annotation that would accept an array of Strings and then just annotates each of them with @Path and duplicates the method which was annotated with this. Think this;

public @interface Paths {
    public String[] value();
    for String value : values {
        @Path(value)
    }
}

This, of course, does not work for many reasons, (one being that annotations need to be processed) but this question is more if this is even possible. A proof of concept so to speak.

SirHawrk
  • 610
  • 1
  • 4
  • 17
  • 1
    You can do that with annotation processing. You'll need to create your own Paths annotation (like you did but without the for loop), then when you compile the code with annotation processing, you will get the method annotated with Paths and generate a different method with a single Path annotation for each value. It is feasible. Even though if you only have 12 and this number is not dynamically changing, you'd spend less time in duplicating the methods manually and call always the same factorized method. – Matteo NNZ Jul 25 '22 at 09:40
  • `@Path` and `@PathParam` accept regular expression (https://docs.jboss.org/resteasy/docs/6.1.0.Beta3/userguide/html_single/index.html#_Path_and_regular_expression_mappings) and may be useful – Luca Basso Ricci Jul 25 '22 at 14:17
  • @LucaBassoRicci I am using ```@javax.ws.rs.Path``` these appear not to use regex – SirHawrk Jul 25 '22 at 14:40
  • AFAIK Quarkus allows regex in `@Path` annotation – Luca Basso Ricci Jul 26 '22 at 06:50

1 Answers1

1

You want multiple @PathParam annotations. You can write these directly in Java, using the repeating annotations feature. There is a slightly better explanation at Baeldung.

You will still need to make your annotation processor cognizant of the wrapper annotation. It is named as the argument to the @Repeatable meta-annotation.

mernst
  • 7,437
  • 30
  • 45