java – 如何使用Spring RestTemplate在POST中传递数组?
我使用Spring的RestTemplate在POST中传递数组时遇到了困难.以下是我使用的代码:
我在这里调用RestTemplate:
private static void sendEntries() {
RestTemplate restTemplate = new RestTemplate();
String uri = "http://localhost:8080/api/log/list.json";
// Both LogEntry and ExceptionEntry extend Entry
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));
}
控制器包含:
@RequestMapping(value = "api/log/list", method = RequestMethod.POST)
public @ResponseBody Entry[] saveList(@RequestBody Entry[] entries) {
for (Entry entry : entries) {
entry = save(entry);
}
return entries;
}
这导致:
org.springframework.web.client.HttpClientErrorException: 400 Bad Request
它看起来不像是将数组添加到请求中.当我不尝试传递数组时,所有其他POST请求都有效.我只是不确定我需要做什么才能使数组正确传递.
这是正确的做法吗?是否可以通过集合?