spring @RequestBody MultiValueMap 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33796218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap
提问by Somasundaram Sekar
Based on the answer for problem with x-www-form-urlencoded with Spring @Controller
基于使用 Spring @Controller 解决 x-www-form-urlencoded 问题的答案
I have written the below @Controller method
我写了下面的@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");
}
}
the request to which fails with the below error
请求失败并出现以下错误
{
"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 was far more friendly, but couldn't use it now given the practical restrictions here]
[PS:Jersey 更友好,但鉴于这里的实际限制,现在无法使用它]
回答by Douglas Ribeiro
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 @RequestBodyannotation.
问题是,当我们使用application/x-www-form-urlencoded 时,Spring 不会将其理解为 RequestBody。因此,如果我们想使用它,我们必须删除@RequestBody注释。
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
请注意,删除了注释@RequestBody
answer: Http Post request with content type application/x-www-form-urlencoded not working in Spring
答案:内容类型为 application/x-www-form-urlencoded 的 Http Post 请求在 Spring 中不起作用
回答by Scadge
It seems that now you can just mark the method parameter with @RequestParam
and it will do the job for you.
看来现在您只需标记方法参数,@RequestParam
它就会为您完成这项工作。
@PostMapping( "some/request/path" )
public void someControllerMethod( @RequestParam Map<String, String> body ) {
//work with Map
}
回答by Agustin Almonte
Add a header to your request to set content type to application/json
向您的请求添加标头以将内容类型设置为 application/json
curl -H 'Content-Type: application/json' -s -XPOST http://your.domain.com/ -d YOUR_JSON_BODY
this way spring knows how to parse the content.
这样 spring 就知道如何解析内容。
回答by Edgardo Genini
In Spring 5
在春天 5
@PostMapping( "some/request/path" )
public void someControllerMethod( @RequestParam MultiValueMap body ) {
// import org.springframework.util.MultiValueMap;
String datax = (String) body .getFirst("datax");
}
回答by Hamid Mohayeji
Simply removing @RequestBody
annotation solves the problem (tested on Spring Boot 2):
只需删除@RequestBody
注释即可解决问题(在 Spring Boot 2 上测试):
@RestController
public class MyController {
@PostMapping
public void method(@Valid RequestDto dto) {
// method body ...
}
}
回答by Marco Blos
I wrote about an alternative in this StackOverflow answer.
我在这个 StackOverflow answer 中写了一个替代方案。
There I wrote step by step, explaining with code. The short way:
在那里我一步一步写,用代码解释。简短的方法:
First: write an object
第一:写一个对象
Second: create a converter to mapping the model extending the AbstractHttpMessageConverter
第二:创建一个转换器来映射扩展 AbstractHttpMessageConverter 的模型
Third: tell to spring use this converter implementing a WebMvcConfigurer.class overriding the configureMessageConverters method
第三:告诉 spring 使用此转换器实现 WebMvcConfigurer.class 覆盖 configureMessageConverters 方法
Fourthand final: using this implementation setting in the mapping inside your controller the consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE and @RequestBody in front of your object.
第四个也是最后一个:在控制器内部的映射中使用此实现设置,在对象前面消耗 = MediaType.APPLICATION_FORM_URLENCODED_VALUE 和 @RequestBody。
I'm using spring boot 2.
我正在使用弹簧靴 2。
回答by Crenguta S
Instead of using a Map, you can use the parameters directly:
您可以直接使用参数,而不是使用 Map:
@RequestMapping(method = RequestMethod.POST, value = "/event/register")
@ResponseStatus(value = HttpStatus.OK)
public void registerUser(@RequestParam(name = EVENT_ID) String eventId,
@RequestParam(name = ATTENDEE_ID) String attendeeId,
@RequestParam(name = SCENARIO) String scenario) {
log.info("Register user: eventid: {}, attendeeid: {}, scenario: {} ", eventId,attendeeId,scenario);
//more code here
}