简体   繁体   中英

Handling dynamic query parameter in CXF restful webservice

I want to handle dynamic query parameter in CXF RESTful service.

My concern here is from the service end i will not be knowing the number of parameters/key names that comes with the request. Could you please let me know how we can handle this situation/scenario.

For example my client code will look like below ,

Map<String, String> params = new HashMap<>();
params.put("foo", "hello");
params.put("bar", "world");

WebClient webClient = WebClient.create("http://url"); 
for (Entry<String, String> entry : params.entrySet()) {
    webClient.query(entry.getKey(), entry.getValue());
}

Response res = webClient.get(); 

The below code works fine for me,

public String getDynamicParamter(@Context UriInfo ui){
    System.out.println(ui.getQueryParameters());
    MultivaluedMap<String, String> map = ui.getQueryParameters();
    Set keys=map.keySet();
    Iterator<String > it = keys.iterator();
    while(it.hasNext()){
        String key= it.next();
        System.out.println("Key --->"+key+"  Value"+map.get(key).toString());
    }
    return "";

}

However , could you please let me know the below,

  1. Whether it is standard way?
  2. And, is there any other ways to achieve it?

CXF version : 3.1.4

Getting the query parameters from the UriInfo

As you already guessed, there are some methods in UriInfo API that will provide you the query parameters:

  • getQueryParameters() : Returns an unmodifiable map containing the names and values of query parameters of the current request. All sequences of escaped octets in parameter names and values are decoded, equivalent to getQueryParameters(true) .
  • getQueryParameters(boolean) : Returns an unmodifiable map containing the names and values of query parameters of the current request.

The UriInfo can be injected in a resource class member using the @Context annotation:

@Path("/foo")
public class SomeResource {

    @Context
    private UriInfo uriInfo;

    @GET
    public Response someMethod() {
        MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
        ...
    }
}

And can be injected in a method parameter:

@GET
public Response someMethod(@Context UriInfo uriInfo) {
    MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
    ...
}

To get the unparsed query string, do the following:

@GET
public Response someMethod(@Context UriInfo uriInfo) {
    String query = uriInfo.getRequestUri().getQuery();
    ...
}

Getting the query parameters from the HttpServletRequest

You could achieve a similiar result by injecting the HttpServletRequest with the @Context annotation, just like the UriInfo mentioned above. Here are a few methods that can be useful:

  • getParameterMap() : Returns an immutable map containing the names and values of parameters sent in the request.
  • getParameterNames() : Returns an Enumeration containing the names of the parameters contained in the request.
  • getParameterValues(String) : Returns an array containing all of the values the given request parameter.
  • getParameter(String) : Returns the value of a request parameter. Should only be used when you are sure the parameter has only one value. Otherwise, only the first value for the given parameter name will be returned.
  • getQueryString() : Returns the query string that is contained in the request URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM