Issue
im working ina spring project and using restemplate to call POST API that take a List of DTOOne And return a List of DTOTWO.
this is my code :
List<MYDTOONE> listDTORequest;
//values..
ResponseEntity<List<MYDTOOTWO>> userActionsResponse = restTemplate.postForEntity("my yrl",
listDTORequest, MYDTOOTWO.class);
im geeting a Syntax error in the last param i need to know how i can tell postForEntity that im waiting for List.
i tried also List<MYDTOOTWO>
in the last paramater but still syntax error
thanks in advance.
Solution
You can try to use the following new ParameterizedTypeReference<List< MyDtoTwo>>() {}
instead of MYDTOOTWO.class (MyDtoTwo written for ease of reading) with the exchange
method (UPDATE: postForEntity has parameterizedTypeReference removed).
What happens in your case is the response you get back is parsed as MYDTOOTWO.class however you're trying to interpret the result as a list, resulting in a mismatch. With parameterized type reference, you can specify that you're expecting a list of MYDTOOTWO.class instead.
Your call should be:
ResponseEntity<List<MYDTOOTWO>> userActionsResponse = exchange("my yrl", HttpMethod.POST,
listDTORequest, new ParameterizedTypeReference<List< MYDTOOTWO>>() {});
List<MYDTOTWO> body = userActionsResponse.getBody();
UPDATE (as suggested by OP): If you notice, you will need to send the HttpMethod.POST when making a POST request (as the second argument) and wrap your headers and request data in an HttpEntity requestEntity object as follows:
HttpEntity<Object> requestEntity = new HttpEntity<>(listDTORequest, headers);
Answered By - sbsatter
Answer Checked By - David Marino (JavaFixing Volunteer)