How can I capture all request parameters in JaxRS in Jersey?

I am creating a common web service and should capture all the request parameters in one line for further parsing. How can i do this?

+84
java jersey jax-rs
Apr 19 '11 at 15:09
source share
3 answers

You can access one parameter through @QueryParam("name") or all parameters through a context:

 @POST public Response postSomething(@QueryParam("name") String name, @Context UriInfo uriInfo, String content) { MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); String nameParam = queryParams.getFirst("name"); } 

The key is the @Context jax-rs annotation , which you can use to access:

UriInfo, Request, HttpHeaders, SecurityContext, Providers

+149
Apr 19 '11 at 3:30 p.m.
source share

The unparsed part of the request URI request can be obtained from the UriInfo object:

 @GET public Representation get(@Context UriInfo uriInfo) { String query = uriInfo.getRequestUri().getQuery(); ... } 
+32
May 29 '13 at 9:31
source share

Adding a bit more to the accepted answer. You can also get all query parameters as follows, without adding an additional parameter to the method, which can be useful in documenting.

 @Context private UriInfo uriInfo; @POST public Response postSomething(@QueryParam("name") String name) { MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); String nameParam = queryParams.getFirst("name"); } 

link

0
Jan 25 '19 at 8:55
source share



All Articles