Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
a-w-c opened this issue Apr 22, 2025 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@a-w-c
Copy link

a-w-c commented Apr 22, 2025

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 22, 2025
@rstoyanchev rstoyanchev self-assigned this Apr 24, 2025
@rstoyanchev
Copy link
Contributor

I don't think there is a reason for that, it's just an oversight. I'll have a look at making that work.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 24, 2025
@rstoyanchev rstoyanchev added this to the 6.2.7 milestone Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants