A @RestController
like
@RestController
public class DemoController {
@PostMapping
public Map<String, Object> getAttributes(@RequestBody Body body, ServerWebExchange serverWebExchange) {
return serverWebExchange.getAttribute(body.getAttribute());
}
}
is expected to handle requests with content type application/json. When the client request has a content type x-www-form-urlencoded, I would expect that the server returns 415 Unsupported Media Type or another client error. Instead Spring throws java.lang.IllegalStateException: In a WebFlux application, form data is accessed via ServerWebExchange.getFormData().
A sample application with failing test can be found here: https://github.com/huberchrigu/spring-reactive-wrong-content-type
Comment From: rstoyanchev
Technically it is possible to read the body via FormHttpMessageReader
but we decided to prevent it as part of #22486 in order to avoid running into the issue of trying to consume the body twice, once via ServerWebExchange#getFormData
(e.g. in a filter or other) and a second via @RequestBody
. You're right though that 415 would be a better way to flag this since this effectively means that method doesn't support it.