0

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.

  • have a look at this, it explains how to do a POST with `RestTemplate`: https://www.baeldung.com/spring-resttemplate-post-json – stringy05 Nov 26 '19 at 02:57
  • this can also be used for `post`. – bananas Nov 26 '19 at 04:31
  • Hi @emotionlessbananas , Can you tell me how to pass input POST body payload to restClient.exchange method (3rd parameter) ? – Sai Krishna Nov 27 '19 at 04:31
  • If you can form a clean example. what you want exactly and where you are getting stuck. You will get more help from community. Otherwise no one is sure how abstract things you are looking for – bananas Nov 27 '19 at 06:37
  • Sure. Edited my question and added sample code which am trying and where exactly am facing the issue. – Sai Krishna Nov 28 '19 at 04:46

0 Answers0