Java 向端点发送JSON不会';行不通

Java 向端点发送JSON不会';行不通,java,spring,rest,resttemplate,Java,Spring,Rest,Resttemplate,想法:我想调用一个生成ArrayList的方法。一旦准备好了,我想将这个列表重定向到给定的端点 用于生成ArrayList的类 @GetMapping("/createcustomer/{start}/{end}") @ResponseBody public ArrayList<Customer> createCustomer(@PathVariable int start, @PathVariable int end) { ArrayList

想法:我想调用一个生成
ArrayList
的方法。一旦准备好了,我想将这个列表重定向到给定的端点

用于生成ArrayList的类

    @GetMapping("/createcustomer/{start}/{end}")
    @ResponseBody
    public ArrayList<Customer> createCustomer(@PathVariable int start, @PathVariable int end) {
        ArrayList<Customer> customerList = new ArrayList<Customer>();
        IntStream.range(start, end).parallel().forEach(index -> {
            customerList.add(generateCustomer());
        });

        RestTemplate restTemplate = new RestTemplate();
        String callbackURL = "http://localhost:8080/customerListEndpoint";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<?> entity = new HttpEntity<>(headers);
        restTemplate.exchange(callbackURL, HttpMethod.POST, entity, ArrayList.class);
        return customerList;
    }
@PostMapping("/customerListEndpoint")
     public void createCustomer(@RequestBody ArrayList<Customer> arrayList) {
        for(int i=0; i<arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
     }
错误:

2018-04-22 16:28:33.203 ERROR 1672 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 null] with root cause

org.springframework.web.client.HttpClientErrorException: 400 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:772) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:725) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:681) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:597) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE]
    at path.CustomerCreationRestService.createCustomer(CustomerCreationRestService.java:65) ~[classes/:na]

您可以像这样在HTTP实体中发送请求正文和头

ObjectMapper mapper = new ObjectMapper();
String requestJson = mapper.writeValueAsString(customerList);
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
ObjectMapper mapper=new ObjectMapper();
String requestJson=mapper.writeValueAsString(customerList);
HttpEntity=新的HttpEntity(requestJson,headers);

您可以在HTTP实体中发送请求正文和头,如下所示

ObjectMapper mapper = new ObjectMapper();
String requestJson = mapper.writeValueAsString(customerList);
HttpEntity<String> entity = new HttpEntity<String>(requestJson, headers);
ObjectMapper mapper=new ObjectMapper();
String requestJson=mapper.writeValueAsString(customerList);
HttpEntity=新的HttpEntity(requestJson,headers);

您实际上没有将客户列表传递给您的呼叫。您可以使用HttpEntity构造函数,但实际上您没有将customerList传递给您的调用。您可以使用HttpEntity构造函数