Spring Boot HttpClient错误异常$错误请求:400:调用restTemplate.postForObject时[没有正文]

zz2j4svz  于 2022-12-26  发布在  Spring
关注(0)|答案(1)|浏览(230)

我正在调用在SpringBoot中编写的POST服务getOrder3,该服务运行良好(在Postman中测试),但在从另一个服务通过restTemplate. postForObject调用时出现错误。我尝试了2个版本的客户端服务getOrderClientgetOrderClient2,但两者都出现相同的错误:

    • HttpClient错误异常$错误请求:400:[没有人]**

请在下面查找详细信息。如有任何帮助,我们将不胜感激。

    • 获取订单3**
@PostMapping(value="/getOrder3/{month}",produces="application/json")
    public ResponseEntity<OrderResponse> getOrder3(
        @PathVariable("month") String month,
        @RequestParam String parmRequestSource,
        @RequestParam(required=false) String parmAudienceType,
        @RequestBody OrderRequestForm orderRequestForm) {

        OrderResponse orderResponse = new OrderResponse();

        log.info("In getOrder3...parmRequestSource = " + parmRequestSource + " parmAudienceType = " + parmAudienceType);

            try {

                    //validate JSON schema
                    //orderService.validateMessageAgainstJSONSchema(orderRequestForm);

                    //process order
                    orderResponse = orderService.processOrder(orderRequestForm);

                    orderResponse.setParmRequestSource(parmRequestSource);
                    orderResponse.setParmAudienceType(parmAudienceType);
                    orderResponse.setMonth(month);

            }catch (Exception e) {
                throw new OrderException("101", e.getMessage(), HttpStatus.BAD_REQUEST);
            }   

        return new ResponseEntity<>(orderResponse,HttpStatus.OK);
    }

这项服务运行良好,在 Postman 测试

现在,当我尝试通过restTemplate.postForObject调用另一个微服务时,我收到了错误。尝试了以下2个版本的客户端,getOrderClientgetOrderClient2

    • 获取订单客户端**
@PostMapping(value="/getOrderClient/{month}",produces="application/json")
    public OrderResponse getOrderClient(
            @PathVariable("month") String month,
            @RequestParam String parmRequestSource,
            @RequestParam String parmAudienceType,
            @RequestBody OrderRequestForm orderRequestForm) throws URISyntaxException, JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();

        URI uri = new URI("http://localhost:51001/orders/v1/getOrder/"+month+"?parmRequestSource="+parmRequestSource+"&parmAudienceType="+parmAudienceType);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        String requestJson  = new ObjectMapper().writeValueAsString(orderRequestForm);

        HttpEntity<String> httpEntity = new HttpEntity<String>(requestJson,headers);
        String response =  restTemplate.postForObject(uri, httpEntity, String.class);

        return new ObjectMapper().readValue(response, OrderResponse.class);
    }
    • 获取订单客户端2**
@PostMapping(value="/getOrderClient2/{month}",produces="application/json")
        public OrderResponse getOrderClient2(
                @PathVariable("month") String month,
                @RequestParam String parmRequestSource,
                @RequestParam String parmAudienceType,
                @RequestBody OrderRequestForm orderRequestForm) throws URISyntaxException, JsonProcessingException {
            RestTemplate restTemplate = new RestTemplate();

            URI uri = new URI("http://localhost:51001/orders/v1/getOrder/"+month+"?parmRequestSource="+parmRequestSource+"&parmAudienceType="+parmAudienceType);

            return restTemplate.postForObject(uri, orderRequestForm, OrderResponse.class);
        }

两者均给出相同的错误:

    • HttpClient错误异常$错误请求:400:[没有人]**

请建议。

k10s72fa

k10s72fa1#

为了提高解决方案的可见性,@astar通过使用@JsonProperty注解模型对象的属性来修复这个问题。

相关问题