Java 如何使用SpringRESTTemplate在POST中传递数组?
我在使用Spring的RestTemplate传递帖子中的数组时遇到困难。以下是我正在使用的代码: 我在这里调用RestTemplate:Java 如何使用SpringRESTTemplate在POST中传递数组?,java,spring,api,rest,resttemplate,Java,Spring,Api,Rest,Resttemplate,我在使用Spring的RestTemplate传递帖子中的数组时遇到困难。以下是我正在使用的代码: 我在这里调用RestTemplate: private static void sendEntries() { RestTemplate restTemplate = new RestTemplate(); String uri = "http://localhost:8080/api/log/list.json"; // Both LogEntry and Except
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
看起来没有将数组添加到请求中。当我不试图传递数组时,所有其他请求后工作。我只是不知道我需要做什么才能让数组正确地通过
这是正确的做法吗?是否可以改为传递集合 您可以查看此帖子:,该帖子的解决方案是:
可以使用RestTemplate的postForObject方法发布列表或其他类型的对象。我的解决方案如下:
控制器:
@RequestMapping(value="/getLocationInformations", method=RequestMethod.POST)
@ResponseBody
public LocationInfoObject getLocationInformations(@RequestBody RequestObject requestObject)
{
// code block
}
创建用于过帐到服务的请求对象:
public class RequestObject implements Serializable
{
public List<Point> pointList = null;
}
public class Point
{
public Float latitude = null;
public Float longitude = null;
}
使用请求对象发布点列表并从服务获取响应对象:
public class ResponseObject implements Serializable
{
public Boolean success = false;
public Integer statusCode = null;
public String status = null;
public LocationInfoObject locationInfo = null;
}
String apiUrl = "http://api.website.com/service/getLocationInformations";
RequestObject requestObject = new RequestObject();
// create pointList and add to requestObject
requestObject.setPointList(pointList);
RestTemplate restTemplate = new RestTemplate();
ResponseObject response = restTemplate.postForObject(apiUrl, requestObject, ResponseObject.class);
// response.getSuccess(), response.getStatusCode(), response.getStatus(), response.getLocationInfo() can be used
如何发布阵列:
private String doPOST(String[] array) {
RestTemplate restTemplate = new RestTemplate(true);
//add array
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://my_url");
for (String item : array) {
builder.queryParam("array", item);
}
//another staff
String result = "";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity =
new HttpEntity<>(headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(
builder.build().encode().toUri(),
HttpMethod.POST,
requestEntity,
String.class);
HttpStatus statusCode = responseEntity.getStatusCode();
if (statusCode == HttpStatus.ACCEPTED) {
result = responseEntity.getBody();
}
return result;
}
在服务器端:
public class MyServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse response) {
try {
String[] array = req.getParameterValues("array");
String result = doStaff(array);
response.getWriter().write(result);
response.setStatus(HttpServletResponse.SC_ACCEPTED);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
}
可能重复的
public class MyServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest req, HttpServletResponse response) {
try {
String[] array = req.getParameterValues("array");
String result = doStaff(array);
response.getWriter().write(result);
response.setStatus(HttpServletResponse.SC_ACCEPTED);
} catch (Exception e) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}
}