I find it difficult to pass an array to POST using Spring RestTemplate. The following is the code I'm using:
I call RestTemplate here:
private static void sendEntries() {
RestTemplate restTemplate = new RestTemplate();
String uri = "http://localhost:8080/api/log/list.json";
LogEntry entry1 = new LogEntry();
ExceptionException entry2 = new ExceptionEntry();
Entry[] entries = {entry1, entry2};
entries = restTemplate.postForObject(uri, entries, Entry[].class);
System.out.println(new Gson().toJson(entries));
}
And the controller contains:
@RequestMapping(value = "api/log/list", method = RequestMethod.POST)
public @ResponseBody Entry[] saveList(@RequestBody Entry[] entries) {
for (Entry entry : entries) {
entry = save(entry);
}
return entries;
}
The result is:
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
It doesn't look like the array is being added to the request. All other POST requests work when I am not trying to pass an array. I'm just not sure what I need to do to make the array pass correctly.
Is this the right way to do this? Is it possible to transfer the collection?
source
share