My question is similar to below question,
Generics with Spring RESTTemplate
The above solution is for a GET call, but am looking for a POST call and in my case input payload is also a wrapper object with generic.
This is how my controller function looks like:
ResponseWrapper<ResponseModel> apiForPost(
@RequestBody RequestWrapper<RequestModel> inputPayload,
@Context HttpHeaders headers) {
}
This is the sample code am trying and where am facing the issue
RequestWrapper<RequestModel> request = new RequestWrapper<>();
//code to set above request object
String url ="http://some_url";
RestTemplate restTemplate = new RestTemplate();
ResponseWrapper<ResponseModel> response = restTemplate.exchange(url,
HttpMethod.POST,
request,
new ParameterizedTypeReference<ResponseWrapper<ResponseModel>>() {}).getBody(); // shows error at that the 3rd parameter (request) cannot be of that type
As per the documentation this is what exchane method expects:
exchange(URI url, HttpMethod method, HttpEntity requestEntity, ParameterizedTypeReference responseType)
(https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html)
If anyone can help me on this, that would be great. Thanks.