gpt4 book ai didi

spring - @RequestBody MultiValueMap 不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8'

转载 作者:IT老高 更新时间:2023-10-28 13:01:54 32 4
gpt4 key购买 nike

基于答案 for problem with x-www-form-urlencoded with Spring @Controller

我写了下面的@Controller方法

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
, produces = {"application/json", "application/xml"}
, consumes = {"application/x-www-form-urlencoded"}
)
public
@ResponseBody
Representation authenticate(@PathVariable("email") String anEmailAddress,
@RequestBody MultiValueMap paramMap)
throws Exception {


if(paramMap == null || paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
}

请求失败并出现以下错误

{
"timestamp": 1447911866786,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/users/usermail%40gmail.com/authenticate"
}

[PS:Jersey 友好得多,但考虑到这里的实际限制,现在无法使用它]

最佳答案

问题是当我们使用 application/x-www-form-urlencoded 时,Spring 并不将其理解为 RequestBody。所以,如果我们想使用这个我们必须删除 @RequestBody 注释。

然后尝试以下操作:

@RequestMapping(
path = "/{email}/authenticate",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = {
MediaType.APPLICATION_ATOM_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE
})
public @ResponseBody Representation authenticate(
@PathVariable("email") String anEmailAddress,
MultiValueMap paramMap) throws Exception {

if (paramMap == null &&
paramMap.get("password") == null) {
throw new IllegalArgumentException("Password not provided");
}
return null;
}

注意去掉了注释@RequestBody

回答:Http Post request with content type application/x-www-form-urlencoded not working in Spring

关于spring - @RequestBody MultiValueMap 不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796218/

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