GoldenSugar GoldenSugar - 5 months ago 18
Java Question

Too many javax Params

We have APIs that have number of @DefaultValue and @QueryParam running into double digits. Is there any elegant way of reducing the cutter on the argument list? Can we separately specify the mapping elsewhere and inject a single elegant object?

public Response doRestfulAPI(@QueryParam("param1") String param1,
@QueryParam("param2") String param2,
@QueryParam("param3") String param3,
.......
@QueryParam("paramN") String paramN)


The above, we like to make it more elegant like:

public Response doRestfulAPI(@QueryParam("complex") Complex complexParams)


Is something like the above possible?

Answer

I assume you are using JAX-RS. In this case you should hava a look at @BeanParam:

 public class MyBean {
   @QueryParam("param1")
   private String param1;

   @QueryParam("param2")
   private String param2;

   ...
 }

In your resource class:

 public Response doRestfulAPI(@BeanParam MyBean bean) {
   ...
 }
Comments