Hi I am using resteasy api, I need to use Map as QueryParam. I can use list as QueryParam but when I try to pass Map I get the error mentioned below.
Here is my service code
@GET
@Path("/movies")
@Produces(MediaType.APPLICATION_JSON)
public SolrDocumentList getPropertiesByKeyword1(@QueryParam("filterMap") final Map<String,String> list)
{}
[org.jboss.resteasy.core.ExceptionHandler] (http-/0.0.0.0:18080-1) Failed executing POST /solr/search/properties: org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [map type; class java.util.LinkedHashMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] from JSON String; no single-String constructor/factory method (through reference chain: com.rentr.solr.SearchRequestDto["filterMap"])
at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:183) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:88) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:111) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.6.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.6.Final.jar:]
Can we use Map as QueryParam, If yes then how?
Best Answer
The injected value will not be a
Map
. With aMap
, you are suggesting that there will be different keys, when in actuality, there is only one,"filter"
. So instead of aMap
, you use aList<String>
Another alternative, is to get all the query params from the
UriInfo
(seems unnecessary, just showing you options :-)From the
queryMap
, you can attempt to get an query parameter values by key, and it will return a list of the values for that key