簡體   English   中英

resteasy:如何在resteasy服務api中將Map用作QueryParam

[英]resteasy : How to consume Map as QueryParam in resteasy service api

嗨,我正在使用resteasy api,我需要使用Map作為QueryParam。 我可以將list用作QueryParam,但是當我嘗試傳遞Map時,出現以下錯誤。

這是我的服務代碼

@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)執行POST / solr / search / properties失敗:org.jboss.resteasy.spi.ReaderException:org.codehaus.jackson.map .JsonMappingException:無法實例化[map type; JSON字符串中的類java.util.LinkedHashMap,[簡單類型,類java.lang.String]-> [簡單類型,類java.lang.String]]; org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:183)上沒有單字符串構造函數/工廠方法(通過參考鏈:com.rentr.solr.SearchRequestDto [“ filterMap”]) 3.0.6.Final.jar:]位於org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:88)[resteasy-jaxrs-3.0.6.Final.jar:]位於org.jboss.resteasy.core .MethodInjectorImpl.invoke(MethodInjectorImpl.java:111)[resteasy-jaxrs-3.0.6.Final.jar:]在org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280)[resteasy-jaxrs-3.0 .6.Final.jar:]位於org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234)[resteasy-jaxrs-3.0.6.Final.jar:]位於org.jboss.resteasy.core。 ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221)[resteasy-jaxrs-3.0.6.Final.jar:]在org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356)[resteasy-jaxrs-3.0。 6.Final.jar:]在org.jboss .resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)[resteasy-jaxrs-3.0.6.Final.jar:]位於org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java: 220)[resteasy-jaxrs-3.0.6.Final.jar:]

我們可以將Map用作QueryParam嗎?

注入的值將不是Map 對於Map ,建議您將有不同的鍵,而實際上只有一個"filter" 因此,您可以使用List<String>而不是Map

@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"
結果: Filter Params: hello,world,


另一種選擇是從UriInfo獲取所有查詢參數(似乎不必要,僅向您顯示選項:-)

@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();
}

queryMap ,您可以嘗試通過鍵獲取查詢參數值,它將返回該鍵的值列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM