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();
}

Comment From: rstoyanchev

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.

Comment From: TGhoul

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?

Comment From: rstoyanchev

Can a function achieve this effect?

No.

Comment From: eatyours0up

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.

Comment From: siqiniao

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

Comment From: aokegbile

Can this issue be reopened and fixed properly?

Comment From: HimanshuChugh2

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

Comment From: dannyZhou

Not working on spring boot 2.3.4.RELEASE ?

Comment From: siqiniao

same issue , good idea ?

Comment From: youngledo

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:

```java @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.

Comment From: licodevone

Perfect @rstoyanchev, thank you very!!!

greetings!

Comment From: hsynff

Same issue. I want to consume both application/json and aplication/x-www-form-urlencoded. It looks very ugly to have 2 identical endpoints with different consume types

Comment From: youngledo

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:

```java @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.

In addition, this method does not limit the placement of request parameters on the URL, such as:

POST /test?username=foo&password=123456 HTTP/1.1
Host: localhost:8888

What I want is that request parameters can only be placed in the request body, not both.

Comment From: sichkarev

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: ```java @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.

In addition, this method does not limit the placement of request parameters on the URL, such as:

POST /test?username=foo&password=123456 HTTP/1.1 Host: localhost:8888

What I want is that request parameters can only be placed in the request body, not both.

Hi, did you find a solution? How can I describe method that send rrequest at body section only?

POST /api/endpoint HTTP/1.1
Host: localhost
Connection: close
Content-Type: application/x-www-form-urlencoded

id=1&id=2&id=3

Comment From: youngledo

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: ```java @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.

In addition, this method does not limit the placement of request parameters on the URL, such as: POST /test?username=foo&password=123456 HTTP/1.1 Host: localhost:8888

What I want is that request parameters can only be placed in the request body, not both.

Hi, did you find a solution? How can I describe method that send rrequest at body section only?

``` POST /api/endpoint HTTP/1.1 Host: localhost Connection: close Content-Type: application/x-www-form-urlencoded

id=1&id=2&id=3 ```

no, spring framework does not support !

Comment From: chanaku

Hi, I working with 2.5.5 version, and this solution isn't help. still get the error: .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported]

Comment From: quiet-ranger

I am on Spring Boot 2.6.6 and facing the same problem. Furthermore, the following cannot be a solution:

@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();
}

Because the method signatures are identical and the compiler will have none of it.

Did anyone come up with a practical working solution?

Comment From: TranTuanNghia94

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:

```java @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.

Both methods have same erasure

Comment From: thinking-github

Can a function achieve this effect?

Comment From: JaservTECH

Any idea ? or good luck of try? facing same condition.

Comment From: swcumt

not work at spring version 5.3.21, does has any other idea?