简体   繁体   中英

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 part was truncated. part被截断了。

  @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.

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();
    ...
}

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)

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.

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