Nel*_*ess 4 java rest spring spring-mvc
有没有一种方法可以获取整个查询字符串而不进行解析?如:
http://localhost:8080/spring-rest/ex/bars?id=100,150&name=abc,efg
Run Code Online (Sandbox Code Playgroud)
我想得到一切吗?作为一个字符串。是的,我稍后会对其进行解析,但这使我的控制器和所有后续代码更加通用。
到目前为止,我已经尝试使用@ PathParam,@ RequestParam以及@Context UriInfo,结果如下。但是我似乎无法理解整个字符串。这就是我要的:
id=100,150&name=abc,efg
Run Code Online (Sandbox Code Playgroud)
使用curl @PathParam使用
http:// localhost:8080 / spring-rest / ex / bars / id = 100,150&name = abc,efg
产生id = 100,150
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring/{qString}")
public String getStuffAsParam ( @PathParam("qstring") String qString) {
...
}
Run Code Online (Sandbox Code Playgroud)
@RequestParam使用
http:// localhost:8080 / spring-rest / ex / bars?id = 100,150&name = abc,efg
给出无法识别的名称。
http:// localhost:8080 / spring-rest / ex / bars?id = 100,150; name = abc,efg
产生异常。
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring")
public String getStuffAsMapping (@RequestParam (value ="qstring", required = false) String[] qString) {
...
}
Run Code Online (Sandbox Code Playgroud)
编辑-下面的方法是我想要关注的。
这几乎可以工作。它没有在MultivaluedMap中提供完整的查询字符串。它只是给我第一个字符串,直到&。我尝试使用其他字符作为分隔符,但仍然无法正常工作。我需要使该字符串处于未解码状态。
@Context与UriInfo使用
http:// localhost:8080 / spring-rest / ex / bars?id = 100,150&name = abc,efg
给出queryParams id = [100,150]的值。再次,name =部分被截断了。
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring")
public String getStuffAsMapping (@Context UriInfo query) {
MultivaluedMap<String, String> queryParams = query.getQueryParameters();
...
}
Run Code Online (Sandbox Code Playgroud)
我在想查询字符串被解码,这并不是我真正想要的。如何获得整个字符串?
任何帮助是极大的赞赏。
您应该查看支持的参数列表:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-methods
您可以添加一个HttpServletRequest
参数并调用getQueryString()
:
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/spring-rest/ex/qstring")
public String getStuffAsMapping(HttpServletRequest request) {
String query = request.getQueryString();
...
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用@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();
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4811 次 |
最近记录: |