gpt4 book ai didi

java - 带有 x-www-form-urlencoded 的 Spring MVC 和 @RequestParam

转载 作者:行者123 更新时间:2023-11-30 08:59:51 25 4
gpt4 key购买 nike

我正在尝试构建一个 API,它接受一个 POST,正文中有一个参数应该是 x-www-form-urlencoded。我目前将其映射为:

 @RequestMapping(method = POST,  consumes = APPLICATION_FORM_URLENCODED_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<LinkEntity> createShortLink(@RequestBody String url) {
LinkEntity savedLink = linkService.create(url);
}

当我使用一个表单参数url= http://www.drissamri.be 使用 Postman(REST Chrome 扩展)进行 POST 时 它作为 url=http%3A%2F%2Fwww.drissamri 进入此方法。如预期的那样。

当我尝试将 @Requestbody 更改为 @RequestParam(value = "url") 时,我仍然得到带有 url= 前缀的 URL(如预期的那样)但是它不再是 urlencoded。为什么不再进行编码?这是一个错误还是有另一种方法可以将参数作为 urlencoded 值接收

最佳答案

正如 Pavel Horal 所正确提及的那样ServletRequest#getParameter 检索参数并且已经解码。如果您需要访问 origin 参数,您可以通过其他方式访问该参数。

一种方法是注入(inject) HttpServletRequest 并使用 HttpServletRequest#getQueryString()解码这些值。

关于java - 带有 x-www-form-urlencoded 的 Spring MVC 和 @RequestParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com