Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting whole query string from REST GET service call

Is there a way to get the whole query string without it being parsed? As in:

http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg

I want to get everything following the ? as one string. Yes I will parse it later, but this allows my controller and all follow-on code to be more generic.

So far I've tried using @PathParam, @RequestParam as well as @Context UriInfo with the results following. But I can't seem to get the whole string. This is what I want:

id=100,150&name=abc,efg

Using curl @PathParam using

http://localhost:8080/spring-rest/ex/bars/id=100,150&name=abc,efg

produces id=100,150

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  @Path("/spring-rest/ex/qstring/{qString}")
  public String getStuffAsParam ( @PathParam("qstring") String qString) { 
         ...
  }

@RequestParam using

http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg

gives name not recognized.

http://localhost:8080/spring-rest/ex/bars?id=100,150;name=abc,efg

produces exception.

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  @Path("/spring-rest/ex/qstring")
  public String getStuffAsMapping (@RequestParam (value ="qstring", required = false) String[] qString) { 
    ...
  }

EDIT - THE APPROACH BELOW IS WHAT I'D LIKE TO FOCUS ON.

This works almost. It doesn't give me the full query string in the MultivaluedMap. It is only giving me the first string up to the &. I've tried using other characters as the delimiter and still doesn't work. I need to get this string in its undecoded state.

@Context with UriInfo using

http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg

gives value for queryParams id=[100,150]. Again the name= part was truncated.

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  @Path("/spring-rest/ex/qstring")
  public String getStuffAsMapping (@Context UriInfo query) { 
      MultivaluedMap<String, String> queryParams = query.getQueryParameters();
    ...
  }

I'm thinking the query string is being decoded which I don't really want. How do I get the whole string?

Any help is greatly appreciated.

like image 623
Nelda.techspiress Avatar asked Mar 10 '16 21:03

Nelda.techspiress


2 Answers

You should have a look at the list of supported parameters:

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-methods

In your case, you can add a HttpServletRequest parameter and call getQueryString():

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring")
public String getStuffAsMapping(HttpServletRequest request) { 
    String query = request.getQueryString();
    ...
}

Another way is to use the @Context UriInfo, then call UriInfo.getRequestUri() followed by URI.getQuery():

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring")
public String getStuffAsMapping(@Context UriInfo uriInfo) { 
    String query = uriInfo.getRequestUri().getQuery();
    ...
}
like image 93
Andreas Avatar answered Oct 30 '22 12:10

Andreas


I would go with

http://localhost:8080/spring-rest/ex/bars?id=100,150;name=abc,efg

and have this RequestMapping

@RequestMapping(value="/spring-rest/ex/bars")
public String getStuffAsParam(@RequestParam("id")String id, @RequestParam("name")String name)
like image 39
dom farr Avatar answered Oct 30 '22 12:10

dom farr