Spring-framework: How to make the content type application/x-www-form-urlencoded work with @RequestBody annotation?

Created on 4 Apr 2019  路  10Comments  路  Source: spring-projects/spring-framework

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 @RequestBodyannotation.

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.

Spring mvc supports the deserialization of application/x-www-form-urlencoded and also supports the deserialization of application/json. Why not do it at the same time? I am very confused.

Is there another way to support these two content-type and like @RequestBody can auto deserialized request body into an Object?

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

Most helpful comment

You can bind form data onto an Object with @ModelAttribute (which is also assumed by default so you don't even have to add it), for example:

@PostMapping(path = "/test", consumes = "application/json")
public String test(@RequestBody User user) {
  return user.toString();
}

@PostMapping(path = "/test", consumes = "application/x-www-form-urlencoded")
public String test(User user) {
  return user.toString();
}

Data binding gives fine-grained control over which fields to bind and which to disallow, along with type formatting control, etc via @InitBinder methods and a BindingResult with field-specific errors.

For form data with @RequestBody it's mostly a MultiValueMap we support.

All 10 comments

You can bind form data onto an Object with @ModelAttribute (which is also assumed by default so you don't even have to add it), for example:

@PostMapping(path = "/test", consumes = "application/json")
public String test(@RequestBody User user) {
  return user.toString();
}

@PostMapping(path = "/test", consumes = "application/x-www-form-urlencoded")
public String test(User user) {
  return user.toString();
}

Data binding gives fine-grained control over which fields to bind and which to disallow, along with type formatting control, etc via @InitBinder methods and a BindingResult with field-specific errors.

For form data with @RequestBody it's mostly a MultiValueMap we support.

To achieve the effect I want, I have to create two @PostMapping functions, one for application/json and one for application/x-www-form-urlencoded. Is this the meaning?

I just tested it, the way to declare two functions can be achieved. Can a function achieve this effect?

Can a function achieve this effect?

No.

I'm running into the same issue, it's kind of redundant to have to duplicate your request mapping if you want to consume application/x-www-form-urlencoded.

I have the same issue too..., why not support?

Can this issue be reopened and fixed properly?

it is not working even when defining two same functions, is there any other workaround for this error:

 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

Not working on spring boot 2.3.4.RELEASE ?

same issue , good idea ?

You can bind form data onto an Object with @ModelAttribute (which is also assumed by default so you don't even have to add it), for example:

@PostMapping(path = "/test", consumes = "application/json")
public String test(@RequestBody User user) {
  return user.toString();
}

@PostMapping(path = "/test", consumes = "application/x-www-form-urlencoded")
public String test(User user) {
  return user.toString();
}

Data binding gives fine-grained control over which fields to bind and which to disallow, along with type formatting control, etc via @InitBinder methods and a BindingResult with field-specific errors.

For form data with @RequestBody it's mostly a MultiValueMap we support.

Whether it's get, post, put or delete, they can send the request body, and there are various types in the request body, such as x-www-form-urlencoded, form data and JSON. Isn't @ request body representing the request body? In this case, why not support x-www-form-urlencoded? I'm very confused.

Was this page helpful?
0 / 5 - 0 ratings