pavel_kazlou
pavel_kazlou

Reputation: 2046

@QueryParam by default on all properties of @BeanParam in jersey 2

I want to use POJO as @BeanParam in jersey 2:

public class GetCompaniesRequest {

    /**  */
    private static final long serialVersionUID = -3264610327213829140L;

    private Long id;
    private String name;
    ...//other parameters and getters/setters
}

@Path("/company")
public class CompanyResource {

    @GET
    public Response getCompanies(
                    @BeanParam final GetCompaniesRequest rq) {
        ...
    }
}

There are many properties in GetCompaniesRequest and I want all them to be available as @QueryParameter. Can I achieve this without putting @QueryParam on every property?

Upvotes: 3

Views: 2604

Answers (1)

tmarwen
tmarwen

Reputation: 16354

You can inject the UriInfo and retrieve all the request parameters from it to a Map.

This will let you avoid injecting multiple query paramters with @QueryParam annotation.

@GET
public Response getCompanies(@Context UriInfo uris) 
{
  MultivaluedMap<String, String> allQueryParams = uris.getQueryParameters();
  //Retrieve the id
  long id = Long.parseLong(allQueryParams.getFirst("id"));
  //Retrieve the name
  String name = allQueryParams.getFirst("name");
  //Keep retrieving other properties...
}

Otherwise, if you still need to use the @BeanParam , you will have to annotate each property in GetCompaniesRequest with @QueryParam:

public class GetCompaniesRequest implements MessageBody 
{
  @QueryParam("id")
  private Long id;
  @QueryParam("name")
  private String name;
  ...
}

Upvotes: 1

Related Questions