I am calling a third-party API that returns two different values for the same parameter name as below,
ActivationResponse activationResponse = new ActivationResponse();
ResponseEntity<ActivationResponse> response = null;
response = restTemplate.exchange(Url, HttpMethod.POST, request, ActivationResponse.class);
activationResponse = response.getBody();
Error response:
{
"executionCode":"2",
"executionMessage":"NullPointerException Occured!",
"response":"java.lang.NullPointerException"
}
Success response:
{
"executionCode" : "0",
"executionMessage" : "SUCCESS",
"response" : {
"orderID" : "79966036"
}
}
As the sample response response
parameter can come as a string or with a JSON object.
Currently the response model is as below,
public class ActivationResponse {
private String executionCode;
private String executionMessage;
private ActivationResponseDetails response;
}
public class ActivationResponseDetails {
private String orderID;
}
When the error comes, an exception is thrown indicating that it can't handle the response
parameter. Please advice how to handle both success and failure scenarios without issues.
Please Note that approach in this answer is not possible, because I have to print the logs in following way, so using @JsonIgnore
will not show that parameter on the log.
Logs are printed like this,
log.info("ActivationResponse json : {}", mapper.writerWithDefaultPrettyPrinter().writeValueAsString(response.getBody()));