1
votes

I am calling Restservice through RestTemplate through below code :

ResponseEntity response = new RestTemplate().postForEntity(url, request, String.class);

whenever something wrong comes from Client , it throws below error :

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:85)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:708)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:661)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:621)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:415)

But when I hit directly the same input request through SOAPUI or Postman ,I get this error like below :

    <Errors>
   <Error ErrorCode="YFS10003" ErrorDescription="YFS:Invalid Order" ErrorUniqueExceptionId="10.148.201.9815264936514610000000000060" HttpCode="400">
      <Attribute Name="ErrorDescription" Value="YFS:Invalid Order"/>
      <Attribute Name="ErrorUniqueExceptionId" Value="10.148.201.9815264936514610000000000060"/>
      <Attribute Name="ErrorCode" Value="YFS10003"/>
      <Attribute Name="HttpCode" Value="400"/>
   </Error>
</Errors>

What I need is , how can I store or catch above exception/error what is coming through client in code and print to have more detailed knowledge about why error came rather than getting a generic error as 400 Bad request.

Is there is way through ResponseErrorHandler class ?

Thanks in advance.

1
It's likely that same end point can return json and xml. When you don't put application/json on the header it might return xml.Try putting header in SOAPUI or POSTMan - cosmos

1 Answers

0
votes

If you take a closer look to the RestTemplate API, you can find the RestTemplate#setErrorHandler method where you can define a custom ErrorHandler and execute the code you want, in your case, print out the message itself.

By default, the RestTemplate is using the DefaultResponseErrorHandler which will simply just throw the exception in case any error happened.