JSON objects in @QueryParam

270 views
Skip to first unread message

Pascal

unread,
Aug 14, 2016, 10:00:45 AM8/14/16
to RestyGWT
Hi, 
I apologize if it's a simple question or if it's been answered before but we've been unable to find anything.

We have a simple log search JAX-RS service, it has two query parameters that are JSON-encoded objects.

@Path("/logs")

public class LogsServiceImpl {

   @Produces(MediaType.APPLICATION_JSON)

   @GET

   public SearchResultDto<ActionLogDto> getActionLogsSearchResult(@QueryParam("filter") LogsFilter filter, @QueryParam("sortColumns") ArrayList<SortColumnDto> sortColumns, @QueryParam("pageSize") int pageSize, @QueryParam("pageNumber") int pageNumber)  {

...


To handle converting the two parameters filter and sortColumns from JSON, We have a simple ParamConverterProvider on the server-side that simply uses ObjectMapper to convert the objects (code below).


On the client-side, we're trying to map this call using RestyGWT.


public interface LogsService extends RestService {

   @GET

   @Path("/logs")

   void getActionLogsSearchResult(@QueryParam("filter") ActionLogsFilter filter, @QueryParam("sortColumns") ArrayList<SortColumnDto> sortColumns, @QueryParam("pageSize") int pageSize, @QueryParam("pageNumber") int pageNumber, MethodCallback<SearchResultDto<ActionLogDto>> callback);


The problem is that without more instructions, RestyGWT seems to simply uses toString() to serialize the two parameters. We'd like to tell the framework to convert the two parameters to JSON to call the service. Is there a way to do this?


Thanks in advance,


Pascal


----


Reference:

ParamConverter we use on the server-side.


@Provider

public class RestProvider implements ParamConverterProvider {

   @Context

   private Providers providers;


   @Override

   public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType, final Annotation[] annotations) {

      // Check whether we can convert the type with Jackson.

      final MessageBodyReader<T> mbr = providers.getMessageBodyReader(rawType, genericType, annotations,

            MediaType.APPLICATION_JSON_TYPE);

      if (mbr == null || !mbr.isReadable(rawType, genericType, annotations, MediaType.APPLICATION_JSON_TYPE)) {

         return null;

      }


      // Obtain custom ObjectMapper for special handling.

      final ContextResolver<ObjectMapper> contextResolver = providers

            .getContextResolver(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);


      final ObjectMapper mapper = contextResolver != null ?

            contextResolver.getContext(rawType) : new ObjectMapper();


            // Create ParamConverter.

            return new ParamConverter<T>() {


               @Override

               public T fromString(final String value) {

                  try {

                     return mapper.reader(rawType).readValue(value);

                  } catch (IOException e) {

                     throw new ProcessingException(e);

                  }

               }


               @Override

               public String toString(final T value) {

                  try {

                     return mapper.writer().writeValueAsString(value);

                  } catch (JsonProcessingException e) {

                     throw new ProcessingException(e);

                  }

               }

            };

   }

}

christian

unread,
Aug 15, 2016, 2:15:19 AM8/15/16
to rest...@googlegroups.com
there is no way to attach some converter to QueryParam annotated parameters. maybe extend ArrayList with a special `toString` method which produces the json instead of the regular.

also a PR for application wide ParamConverter would be nice ;)

- christian


Reply all
Reply to author
Forward
0 new messages