Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RestTemplate postForEntity return List of DTO

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i tried also List<MYDTOOTWO> in the last paramater but still syntax error

thanks in advance.

>Solution :

It may not be the cleanest code, but it is the fastest and simplest way I have found to do it. You read it as an Array and then put it in the List.

List<MYDTOONE> listDTORequest;

// JDK8
List<MYDTOOTWO> userActionsResponse = Arrays.asList(restTemplate.postForEntity("my url", listDTORequest, MYDTOOTWO[].class).getBody());

// JDK9+
List<MYDTOOTWO> userActionsResponse = List.of(restTemplate.postForEntity("my url", listDTORequest, MYDTOOTWO[].class).getBody());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading