Skip to content

UnsupportedMediaTypeException when using WebClient with body that is an Interface #35033

Closed as not planned
@gableg-goto

Description

@gableg-goto

Our team tried to upgrade from Spring Boot 3.4.4 to 3.5.0 and we stumble across a regression that was introduce in Spring-Webflux 2.6.7.

The problem occurs when the defined method use an Interface object as @RequestBody. There is a recent adjustment that was made because of the generic type that wasn't taken in consideration (#34793). With the new modification, we now lose the class of the concret object that is hidden behind the interface. When we try to resolve the right Serializer to use, we find nothing and we get the error :

org.springframework.web.reactive.function.client.WebClientRequestException: Content type 'application/json' not supported for bodyType=com.example.demo.fruit.Fruit

After some digging, I found out that we are now setting the body value with its generic parameter type. Because of this, we are taking the interface as the type instead of the concrete class when we try to resolve the right Serializer.

With the version 6.2.7, there is new variable called bodyValueType that retain the object type (https://github.com/spring-projects/spring-framework/blob/v6.2.7/spring-web/src/main/java/org/springframework/web/service/invoker/HttpRequestValues.java#L80). When this is set, we use this value to determine which Serializer to use (https://github.com/spring-projects/spring-framework/blob/main/spring-web/src/main/java/org/springframework/web/service/invoker/RequestBodyArgumentResolver.java#L113 with the combinaison of https://github.com/spring-projects/spring-framework/blob/main/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/support/WebClientAdapter.java#L140).

To what I can see, this is a breaking change 🤔 and that was passed into a minor. Thankfully, there is a workaround that can be done by using @JsonTypeInfo and @JsonSubTypes on the interface.

I will attach a small project with Spring Boot 3.5.0. You can run the 2 tests to see what is going on (1 will pass and 1 will fail). After that, you could change the Spring Boot version to 3.4.5 (that has webflux 6.2.6) and see that the 2 tests are passing.

regressionDemo.zip

Thank you 🙂

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions