JAX-RS: How to use BeanParam

BeanParam annotation is available in JAX-RS 2.0. It allows us to aggregate the parameters into java bean.

In this post, we are going to see how we can use with an example,

Below is my Resource class.

I have specified the annotation @BeanParam for the SearchParamBean class. Hence the query parameters those are passed to this endpoint will be mapped to the SearchParamBean model class.


import javax.ws.rs.BeanParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class TestResource {

    public Response performSearchWithMultiParameters(@BeanParam SearchParamBean searchParamBean) {
        return Response.ok().entity(searchParamBean).type(MediaType.APPLICATION_JSON).build();


Below is my BeanParam class.

I have given the necessary annotation for all the fields of this class. I have used QueryParam here. Hence the query parameters ‘query’, ‘sort’ and ‘filter’ are mapped to fields of SearchParamBean class.

We can also use PathParam, HeaderParam, CookieParam, MatrixParam and FormParam.


import javax.ws.rs.QueryParam;

public class SearchParamBean {

    public String query;

    public String sort;

    public String filter;

    public String toString() {
        final StringBuilder sb = new StringBuilder("SearchParamBean{");
        sb.append(", sort='").append(sort).append('\'');
        sb.append(", filter='").append(filter).append('\'');
        return sb.toString();


Here is my request and response.

URL: /api/test/search?query=test&sort=test&filter=test

query: "test",
sort: "test",
filter: "test"

Most of the time, we don’t need to use this annotation instead we can use Jackson to map the query parameters directly to a bean class.

For example, When the service accepts more than one query parameters and we want to do any validation and need to use those values further in service layer or DAO layer, then we can make use of this annotation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s