Skip to content
Advertisement

resteasy: @QueryParam to parse nested array structure

I’m using a javascript library called tabulator to display data inside tables on the client side.

The Tabulator js library provides a feature to encode a representation of filters in the query parameters of an ajax request. For example, here’s what the query params look like:

https://host/myEndpoint?size=10&page=1&filters%5B0%5D%5Bfield%5D=username&filters%5B0%5D%5Btype%5D=like&filters%5B0%5D%5Bvalue%5D=filteredBy

Here’s the same url decoded:

https://host/myEndpoint?size=10&page=1&filters[0][field]=username&filters[0][type]=like&filters[0][value]=filteredBy

If possible, I’d like to have a Resteasy endpoint like this:

 @GET
 @Path("/myEndpoint")
 @Consumes("application/json")
 @Produces("application/json")
 public Response myEndpoint(@QueryParam("page") Integer page,
                            @QueryParam("size") Integer size,
                            @QueryParam("filters") List<Filter> filters) {

resteasy interprets page and size no problem, but filters is always a list of size 0.

My Filter bean has 3 fields named field, type, and value with a constructor with single String argument as described here.

But it doesn’t seem like resteasy is recognizing and parsing the filters query param? Is it possible to parse this type of nested array structure query parameters in resteasy?

  filters[0][field]=username&filters[0][type]=like&filters[0][value]=filteredB

Advertisement

Answer

I’m still hoping that there’s a better way, but here’s a possible solution that works for me for now at least:

 @GET
 @Path("/myEndpoint")
 @Consumes("application/json")
 @Produces("application/json")
 public Response myEndpoint(@QueryParam("page") Integer page,
                            @QueryParam("size") Integer size,
                            @Context UriInfo uriInfo) {
    
    for(String key : uriInfo.getQueryParameters().keySet()) {
        
         // check if key starts with something like `filters[0]` 
         // and then parse it however you need. 
    }
 }
Advertisement