Skip to content

RestClient @RequestBody parameters lose generic type information when creating HTTP service beans #34793

Closed
@a-w-c

Description

@a-w-c

We are currently in the process of migrating from FeignClient to the new RestClient from Spring. We are on version 6.2.2 of spring-web for reference.

We have noticed a discrepency from feign when generating beans from HTTP interfaces as seen in https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-interface.

The issue occurs when a method in the interface contains a generic type, e.g.

public interface MyInterface {
    @PostExchange("/mock-endpoint")
    void callMockEndpoint(@RequestBody Set<MyType> myTypeSet);
}

The generic type information for myTypeSet is lost, which causes an issue if MyType.class contains information required for it to be serialised correctly. This does not seem to be the case during deserialisation, where type information is retained.

Looking at HttpServiceMethod , generic type information is present for method parameters when creating the ResponseFunction, but seems to be ignored thereafter.

This means that type information is not present down the line in RestClientAdapter- note the use of RequestBodySpec body(Object body) rather than <T> RequestBodySpec body(T body, ParameterizedTypeReference<T> bodyType), which in our case is required to serialise our request body correctly.

Its difficult from the code to tell whether this information is intentionally dropped. It seems as though generic type information is retained when we serialise a response, so its unclear to me why request parameters are treated differently.

We could fix this by providing a wrapper class to Set<MyType>, but as we also implement this interface in our controllers the knock-on effect of this is quite large.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions