简体   繁体   English

如何防止自动解码 Spring controller 中的 RequestParam 字符串

[英]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.

相关问题 Spring @requestparam如何验证字符串在字符串列表中 - how to Spring @requestparam validate string is in list of strings Spring @RequestParam和控制器接口 - Spring @RequestParam and controller interface 如何在Spring Controller方法中从许多@RequestParam参数切换到@ModelAttribute - How to switch from many @RequestParam arguments to @ModelAttribute in a Spring Controller method 如何使RequestParam动态? - How I can make RequestParam dynamic? 如何处理JSP文件中的@RequestParam? - How can I deal with @RequestParam in JSP file? 我如何使用@RequestParam批注将表单输入获取到控制器 - How do i get form inputs to controller using @RequestParam Annotation 通过@RequestParam Spring @RestController 解码 url 查询参数时遇到问题 - Having a trouble with decoding a url query parameter by @RequestParam Spring @RestController 如何防止控制器方法在Chrome浏览器的Spring Boot中运行两次? - How can I prevent controller methods from running twice in spring boot in chrome browser? 我应该如何在 spring Rest API 中验证 @requestparam 中的依赖参数? - How should I validate dependent parameters in @requestparam in spring Rest API? 如何防止 Spring 数据 JPA 中的自动更新? - How to prevent automatic update in Spring Data JPA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM