0

I have to consume a REST API which can return two different types of responses. this is my code:

    HttpResponse<String> response = client.send(request,
            HttpResponse.BodyHandlers.ofString());
    myObject = objectMapper.readValue(response.body.toString(),MyObject.class);

The problem is that I could receive MyObject or MyError as a response, how should I manage it?

2 Answers 2

2

If when you receive MyError object you also receive an Error HTTP Status you can evaluate that, to parse the response to MyObject or MyError

If you always receive http status 200 you have a bad API design, and you need to do some changes in this API to fix that.

1

You should look for the HTTP response code, and based on the code cast the response to the correct object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.