Search code examples
javaparametersannotationsnamed

Java named/optional parameters using annotation?


In RESTeasy this...

@GET
@Path("request")
public String requestJson(@QueryParam("arg1") @DefaultValue("") String arg1,
                          @QueryParam("arg2") @DefaultValue("0") Integer arg2);

...allows you to define any subset of parameters defined in the method signature. Surely it is then possible to use this same pattern on any method signature something like this:

@Method
public String requestJson(@OptionalParameter("arg1") @DefaultValue("") String arg1,
                          @OptionalParameter("arg2") @DefaultValue("0") Integer arg2);

Is it possible to do this? If so how?


Solution

  • The annotations in the RESTEasy example are possible due to the fact that the object is managed by the RESTEasy framework. It can inspect the method and determine how to invoke it based on those annotations. It is possible to create any annotations you'd want, the key is that the code calling the method needs to know about the annotations and how to process them.