i am using rest template for calling foreign api and add into my dto class.
controller
public ResponseEntity<CountResponse> count(@Validated @RequestBody CountRequest homeRequest){
String uri = “http://localhost:8089/abc?email=" + homeRequest.getEmail();
CountResponse result = restTemplate.getForObject(uri, CountResponse.class);
return ResponseEntity.ok(result);
}
DTO
@Data
public class CountResponse {
@JsonProperty(value = "PendingCount")
private int pendingCount;
@JsonProperty(value = "todayPendingCount")
private int todayPendingCount;
}
i am getting below response when success.
"PendingCount": 1,
"todayPendingCount": 0
}
and getting
{
"error": "No Record found"
}
when not data found ,
How to map error response to my dto class when JSON response is different??
Usually the error will be returned with an http-400 which can me checked before deserialising the error using a specific error class.
If that is not the case your CountResponse should have an error field, either directly or via inheritance.
I suggest inheritance because the api may return different objects for different calls but always the same error structure and it leaves the class definitions looking a little cleaner.