0
点赞
收藏
分享

微信扫一扫

SpringBoot 报错 (‘application/x-www-form-urlencoded;charset=UTF-8‘ not supported)

外贸达人小峻先森 2022-03-11 阅读 40

stackoverflow :

spring - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap - Stack Overflow

高赞:

The problem is that when we use application/x-www-form-urlencoded, Spring doesn't understand it as a RequestBody. So, if we want to use this we must remove the @RequestBody annotation.

Then try the following:

```

@RequestMapping(value = "/{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;
}

```

Note that removed the annotation @RequestBody

说的很明白,移除不使用注解即可。MultiValueMap paramMap就可以读取到application/x-www-form-urlencoded 协议里的数据,类的字段和key相对应,即可匹配读取到类的字段上。

补充链接: 

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

举报

相关推荐

0 条评论