9

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.

Nelda.techspiress
  • 643
  • 12
  • 32

3 Answers3

6

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();
    ...
}
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • This way is nice as you aren't using any spring mapping, but the raw HttpServletRequest object. `public String getStuffAsParam(HttpServletRequest request) { request. getQueryString(); ...}` – dom farr Mar 10 '16 at 21:40
  • How can I do that when I'm using REST services in Spring (@Service) not httpServlet? (See the examples). – Nelda.techspiress Mar 10 '16 at 21:50
  • @Nelda.techspiress You are still running your Spring app in a servlet container, right? All the REST routing and parameter mapping is just Spring using the `HttpServletRequest`, and you can get it by simply asking for it. – Andreas Mar 10 '16 at 23:48
  • @Andreas I tried your suggestion with the UriInfo and it is close but does not give me the whole query string. I still only get id=100,150 when looking at it in the debugger. I don't understand why it isn't reading beyond the &. In fact if I look at the value returned from uriInfo.getRequestUri() it also truncates at the &. – Nelda.techspiress Mar 11 '16 at 00:29
  • I used this solution but had to change my query string to have ; as separator instead of & which is legal and can be referred to as MatrixParams (although I didn't use it as that). – Nelda.techspiress Mar 11 '16 at 20:35
1

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)
dom farr
  • 4,041
  • 4
  • 32
  • 38
  • As I stated in my question, I don't want to have to pull each parameter out of the query string individually. I want to keep the controller autonomous of the content in the query. – Nelda.techspiress Mar 10 '16 at 21:49
1

If you need access to the raw query you need to get it from the request object. Refer to this old question to get access to it. Even though the answer is not accepted it is a well researched response.

Spring 3 MVC accessing HttpRequest from controller

The following code snippet should give the query string once you get access to HttpServletRequest

httpservletrequest.getQueryString()

After I posted this I see @Andreas has posted a similar answer. Accept his answer if the solution helps you.

Community
  • 1
  • 1
Arul
  • 104
  • 4