How do you map multiple query parameters to the fields of a bean on Jersey GET request?

In Jersey 2.0, you’ll want to use BeanParam to seamlessly provide what you’re looking for in the normal Jersey style.

From the above linked doc page, you can use BeanParam to do something like:

@GET
@Path("find")
@Produces(MediaType.APPLICATION_XML)
public FindResponse find(@BeanParam ParameterBean paramBean) 
{
    String prop1 = paramBean.prop1;
    String prop2 = paramBean.prop2;
    String prop3 = paramBean.prop3;
    String prop4 = paramBean.prop4;
}

And then ParameterBean.java would contain:

public class ParameterBean {
     @QueryParam("prop1") 
     public String prop1;

     @QueryParam("prop2") 
     public String prop2;

     @QueryParam("prop3") 
     public String prop3;

     @QueryParam("prop4") 
     public String prop4;
}

I prefer public properties on my parameter beans, but you can use getters/setters and private fields if you like, too.

Leave a Comment