简体   繁体   English

从REST GET服务调用获取整个查询字符串

[英]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. 到目前为止,我已尝试使用@PathParam,@ RequestParam以及@Context UriInfo,结果如下。 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 使用curl @PathParam

http://localhost:8080/spring-rest/ex/bars/id=100,150&name=abc,efg HTTP://本地主机:8080 /弹簧休息/ EX /酒吧/ ID = 100,150&名称= ABC,EFG

produces id=100,150 产生id = 100,150

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

@RequestParam using @RequestParam使用

http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg HTTP://本地主机:8080 /弹簧休息/ EX /条ID = 100,150&名称= ABC,EFG

gives name not recognized. 名称未被识别。

http://localhost:8080/spring-rest/ex/bars?id=100,150;name=abc,efg HTTP://本地主机:8080 /弹簧休息/ EX /条ID = 100,150;名称= 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. 它没有在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 使用UriInfo的@Context

http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg HTTP://本地主机:8080 /弹簧休息/ EX /条ID = 100,150&名称= ABC,EFG

gives value for queryParams id=[100,150]. 给queryParams id = [100,150]赋值。 Again the name= part was truncated. 同样, name = 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 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() : 在您的情况下,您可以添加HttpServletRequest参数并调用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() : 另一种方法是使用@Context UriInfo ,然后调用UriInfo.getRequestUri() ,然后调用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

@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 Spring 3 MVC从控制器访问HttpRequest

The following code snippet should give the query string once you get access to HttpServletRequest 一旦您获得HttpServletRequest的访问权限,以下代码段应该提供查询字符串

httpservletrequest.getQueryString()

After I posted this I see @Andreas has posted a similar answer. 我发布这个后,我看到@Andreas发布了类似的答案。 Accept his answer if the solution helps you. 如果解决方案可以帮助您,请接受他

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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