HttpClientErrorException$BadRequest: 400 : [no body] when calling restTemplate.postForObject

Viewed 8833

I am calling a POST service getOrder3 written in SpringBoot which is working fine (tested in Postman), but getting error when called via restTemplate.postForObject from another service. I tried 2 versions of the client service getOrderClient and getOrderClient2, but both are giving same error :

HttpClientErrorException$BadRequest: 400 : [no body]

Please find the details below. Any help is appreciated.

getOrder3

@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);
    }

The service is working fine , tested in postman

enter image description here

Now when I try to call via another microservice via restTemplate.postForObject, I get the error. Tried 2 versions of the client as below, getOrderClient and getOrderClient2

getOrderClient

@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);
    }

getOrderClient2

@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);
        }

Both are giving same error :

HttpClientErrorException$BadRequest: 400 : [no body]

Please suggest.

1 Answers

To improve the visibility of the solution, @astar fixed the issue by annotating the model object's properties with @JsonProperty.

Related