[英]How can I prevent the automatic decoding of RequestParam Strings in a Spring controller
I have an HTTP GET request controller endpoint where I take in a fileName as a query param and pass that on to another service.我有一个 HTTP GET 请求 controller 端点,我将文件名作为查询参数并将其传递给另一个服务。 For this request the param the filename could include any sort of special characters and I would like to keep these values encoded when passing them on.
对于这个请求,文件名的参数可以包含任何类型的特殊字符,我希望在传递这些值时保持这些值的编码。 2 Characters that have been causing issues are spaces (%20) and +(%2B).
2 引起问题的字符是空格 (%20) 和 +(%2B)。
How can I keep these characters encoded in the request params.如何将这些字符编码在请求参数中。
So far I have tried using the @RequestParam annotation as well as retrieving the params via HttpServletRequest.getParameterValues(String) but both return the decoded values as spaces.到目前为止,我已经尝试使用 @RequestParam 注释以及通过 HttpServletRequest.getParameterValues(String) 检索参数,但两者都将解码后的值作为空格返回。
Any help is appreciated thanks!任何帮助表示感谢!
Yes, these are automatically decoded by the servlet API.是的,这些是由 servlet API 自动解码的。 You should be able to re-encode them -
你应该能够重新编码它们 -
encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8);
I found out that I could get the actual value passed in by using the HttpServletRequest.getQueryString() method.我发现我可以使用 HttpServletRequest.getQueryString() 方法获取传入的实际值。 Parsing this query string I was able to get the un-decoded version of the fileName being passed in. I hope this helps someone in the future.
解析此查询字符串,我能够获得传入的文件名的未解码版本。我希望这对将来的某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.