简体   繁体   English

如何使用@RequestBody注释使内容类型application / x-www-form-urlencoded工作?

[英]How to make the content type application/x-www-form-urlencoded work with @RequestBody annotation?

When the content type is application/json,@RequestBody can read the request body and deserialized into an Object, but the content type is application/x-www-form-urlencoded, spring-mvc can not support this content type with @RequestBody annotation. 当内容类型为application / json时,@ RequestBody可以读取请求体并反序列化为Object,但内容类型为application / x-www-form-urlencoded,spring-mvc不能使用@RequestBody注释支持此内容类型。

In my project, I encountered this problem, I want to support both of them at the same time, but I don't want to lose the convenience of @RequestBody. 在我的项目中,我遇到了这个问题,我想同时支持它们,但我不想失去@RequestBody的便利。

Is there another way to support these two content type and like @RequestBody can auto deserialized request body into an Object? 有没有其他方法来支持这两种内容类型,像@RequestBody可以自动反序列化请求体到一个对象?

@RequestMapping(value = "/test")
public String test(@RequestBody User user) {
  return user.toString();
}

Is there another way to support these two content type and like @RequestBody can auto deserialized request body into an Object? 有没有其他方法来支持这两种内容类型,像@RequestBody可以自动反序列化请求体到一个对象?

Negative. 负。

What you can do is use FormHttpMessageConverter to deserialize into a MultiValueMap<String, String> . 可以做的是使用FormHttpMessageConverter反序列化为MultiValueMap<String, String> You can then write an transformer to take that and create a User object. 然后,您可以编写一个转换器来获取它并创建一个User对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用“ Content-type:application / x-www-form-urlencoded”发出Okhttp请求? - How to make an Okhttp Request with “Content-type:application/x-www-form-urlencoded”? 如何编写控制器类以允许内容类型:application / json和application / x-www-form-urlencoded - How to write controller class to allow content-type: application/json and application/x-www-form-urlencoded 内容类型为application / x-www-form-urlencoded的Http Put请求在Spring中不起作用 - Http Put request with content type application/x-www-form-urlencoded not working in Spring 更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML - Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded Spring Boot - 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8” - Spring Boot - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 使用 RestTemplate 与对象作为数据和 application/x-www-form-urlencoded 内容类型? - Use RestTemplate with object as data and application/x-www-form-urlencoded content type? 内容类型为 application/x-www-form-urlencoded 的 HTTP Post 请求在 Spring 引导服务中不起作用 - HTTP Post request with content type application/x-www-form-urlencoded not working in Spring boot service 请求正文作为 json 发送,即使内容类型设置为 application/x-www-form-urlencoded - The request body is sent as json even though the content type is set as application/x-www-form-urlencoded HttpMediaTypeNotSupportedException:不支持内容类型 'application/x-www-form-urlencoded;charset=UTF-8' - HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported ContentCachingRequestWrapper 只捕获带有 Content-Type:application/x-www-form-urlencoded 的 POST 请求 - ContentCachingRequestWrapper only captures POST request with Content-Type:application/x-www-form-urlencoded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM