Reputation: 171
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?
Upvotes: 1
Views: 9183
Reputation: 209132
The injected value will not be a Map
. With a Map
, you are suggesting that there will be different keys, when in actuality, there is only one, "filter"
. So instead of a Map
, you use a List<String>
@Path("list")
public class QueryParamMapResource {
@GET
public Response getQueryParams(@QueryParam("filter") List<String> params) {
StringBuilder builder = new StringBuilder("Filter Params: ");
for (String value : params) {
builder.append(value).append(",");
}
return Response.ok(builder.toString()).build();
}
}
C:\>curl "http://localhost:8080/test/rest/list?filter=hello&filter=world"
Result:Filter Params: hello,world,
Another alternative, is to get all the query params from the UriInfo
(seems unnecessary, just showing you options :-)
@GET
public Response getQueryParams(@Context UriInfo uriInfo) {
MultivaluedMap<String, String> queryMap = uriInfo.getQueryParameters();
StringBuilder builder = new StringBuilder("Filter Params: ");
List<String> params = queryMap.get("filter");
for (String value : params) {
builder.append(value).append(",");
}
return Response.ok(builder.toString()).build();
}
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
Upvotes: 2