简体   繁体   English

给定特殊字符时,查询参数中缺少整个字符串-Java Spring Boot

[英]Missing entire string in query parameter when given special character - Java Spring Boot

I'm currently writing a Java Spring Boot application with a REST API server. 我目前正在使用REST API服务器编写Java Spring Boot应用程序。 I'm cutting out a lot of non-relevant code here, but one of my POST routes looks like this: 我在这里剪掉了许多无关的代码,但是我的一个POST路由如下所示:

@RequestMapping(value = "/post-request", method = RequestMethod.POST)
public String postRequest(
        @RequestParam(name = REQUEST_STRING) String requestString,
        ){ logger.info(requestString)}

This approach works great for most strings, but I'm getting a really weird error when including special characters in the request parameter. 这种方法适用于大多数字符串,但是当在请求参数中包含特殊字符时,我遇到了一个非常奇怪的错误。 For example, if I send the application with a request containing 例如,如果我向应用程序发送的请求包含

This (&) is an ampersand 这个(&)是&符

within Postman, the application will log 在Postman中,应用程序将记录

This ( 这个 (

and cut out everything else after the special character. 并剪掉特殊字符后的其他所有内容。 I'm fairly certain that this has to do with the way that special characters are encoded within REST API's, especially when it comes to request parameters, but what am I doing wrong here? 我相当确定这与在REST API中对特殊字符进行编码的方式有关,尤其是在涉及请求参数时,但是我在这做错了什么? Is there a way for me to get the full string using Java Spring Boot, or am I restricted from doing this? 有没有办法让我使用Java Spring Boot获取完整的字符串,还是我不能这样做?

RequestParam are diffrentiated with "&" so if you pass "&" directly in string anything after & will be treated as another RequestParam RequestParam以“&”区分,因此,如果直接在字符串中传递“&”,则&之后的任何内容都将被视为另一个RequestParam

eg: 例如:

http://testdata.com?username=test&password=hello&123&type=user http://testdata.com?username=test&password=hello&123&type=user

Here password will be passed as hello and 123 will be treated as anotehr RequestParam 在这里,密码将以hello的形式传递,而123将被视为anotehr RequestParam

To overcome this you need to do encoding for "&" as "%26" 为了解决这个问题,您需要将“&”编码为“%26”

http://testdata.com?username=test&password=hello%26123&type=user http://testdata.com?username=test&password=hello%26123&type=user

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

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