REST Web Service - Dynamic Query Parameters

好久不见. 提交于 2019-12-23 18:11:14

问题


I have a requirement to send dynamic query parameters to REST web service GET method [as shown below].

host:port/app?field1=XXX&value1=VVV&field2=XXX&value2=XXX ....

The consumer can send parameters up to fieldn and valuen. Each field maps to the value.

With this type of requirement, I can't code a finite set of QueryParams on the server side method.

Is there any type of REST library that supports this? I checked RESTEasy and Jersey, and they both don't seem to support this [as far as I checked].

Thanks.


回答1:


Use UriInfo.getQueryParameters(), as following:

@GET
@Path("/foo")
@Produces(MediaType.APPLICATION_JSON)
public Response foo(@Context UriInfo uriInfo) {
    MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); 
    ...
}

It returns a MultivaluedMap. Then just iterate over it.



来源:https://stackoverflow.com/questions/12513307/rest-web-service-dynamic-query-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!