Keetah
Keetah

Reputation: 271

Jersey Service with Object as parameter

I´ve some Jersey services as follows

      @GET
      @Path("/GetUsers")
      @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
      public List<Campania> findUsers(@QueryParam("userName") String User) {
        List<User> users= userBL.getUsers();
        return users;
      }

My problem is that this method should receive 10 parameters, so I would prefer to have only one parameter: GetUsersFilter which contains the 10 parameters.

The only way I know for doint that, is changing from @GET to @POST, but, this services should be a GET. There is another way?

Upvotes: 1

Views: 2030

Answers (1)

isnot2bad
isnot2bad

Reputation: 24444

You can use the @BeanParam annotation to bundle multiple query parameters (and other stuff) into a single java object:

// parameter object that bundles all parameters
public class UserQueryParams {
    @QueryParam("userName")
    private String user;

    @QueryParam("param2")
    private String param2;

    ...
    // getters etc.
}

Then in your JAX-RS resource method, use it as follows:

@GET
@Path("/GetUsers")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public List<User> findUsers(@BeanParam UserQueryParams userQuery) {
    List<User> users = userBL.getUsers(userQuery);
    return users;
}

Upvotes: 3

Related Questions