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

后端 未结 5 1646
傲寒
傲寒 2020-11-30 19:27

A service class has a @GET operation that accepts multiple parameters. These parameters are passed in as query parameters to the @GET service call.

5条回答
  •  一个人的身影
    2020-11-30 20:03

    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.

提交回复
热议问题