Spring RestTemplate Send List an get List
33,107
Solution 1
Check if your code is like below. This should work.
//header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//person list
List<Person> personList = new ArrayList<Person>();
Person person = new Person();
person.setName("UserOne");
personList.add(person);
//httpEnitity
HttpEntity<Object> requestEntity = new HttpEntity<Object>(personList,headers);
ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, requestEntity,new ParameterizedTypeReference<List<Person>>() {});
Solution 2
It may be helpful for you.
List<Integer> officialIds = null;
//add values to officialIds
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
HttpEntity<List<Integer>> request = new HttpEntity<List<Integer>>(officialIds,
headers);
ResponseEntity<YourResponseClass[]> responses =
restTemplate.postForEntity("your URL", request , YourResponseClass[].class );
List<YourResponseClass> list = Arrays.asList(responses.getBody());
Solution 3
I had similar challenge, and here are my 2 cents.
My Controller Class with POST Mapping
@PostMapping("/fetch/projects")
public ResponseEntity<List<Project>> getAllProjects(@RequestBody List<UserProject> userProject) {
// Convert List of projectIds to List of Long using Lambda Functions
List<Long> projectIds = userProject.stream().map(UserProject::getProjectId).collect(Collectors.toList());
List<Project> projectList = projectService.getAllProjectsByProjectIds(projectIds);
log.info("<< Fetching Projects based on Project Ids !");
return new ResponseEntity<List<Project>>(projectList, HttpStatus.OK);
}
My Static URL for the above controller
final String projectBaseUri = "http://localhost:6013/api/v1/project/fetch/projects";
My Caller Service
protected List<Project> getProjectsByIds(List<UserProject> userProject) {
String url = projectBaseUri;
log.info("Project URL : " + url);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> requestEntity = new HttpEntity<Object>(userProject,headers);
ResponseEntity<List<Project>> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,new ParameterizedTypeReference<List<Project>>() {});
return response.getBody();
}
Author by
Mohammad Mirzaeyan
Updated on July 09, 2022Comments
-
Mohammad Mirzaeyan almost 2 years
I want to make a service with Spring's
RestTemplate
, in my service side the code is like this :@PostMapping(path="/savePersonList") @ResponseBody public List<Person> generatePersonList(@RequestBody List<Person> person){ return iPersonRestService.generatePersonList(person); }
In client side if I call the service with this code:
List<Person> p = (List<Person>) restTemplate.postForObject(url, PersonList, List.class);
I can't use the
p
object asList<Person>
, it will become aLinkedHashList
. After some research I find a solution that said I have to call the service with exchange method:ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, personListResult, new ParameterizedTypeReference<List<Person>>() {});
and with this solution the server can't take the object and raise an exception , what's the correct way?