I need to make a JSON API call to an external source inside of my JAX-RS application. From my research, in order to do this I need to use RestClient(), which works. But only when I get a successful response back. It can not parse the body when the status code is anything other than 400. Here's an example of working code when I get a 200 status:
Resource resource = client.resource("https://api.paytrace.com/v1/customer/create");
String response = resource
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + accessToken)
.accept(MediaType.APPLICATION_JSON)
.post(String.class, paytraceJSON);
I'm currently just trying to log out response but get nothing when the response details back an error in the body. Is there a better way to deal with this, so I can echo back errors from the service API I'm using within my own?
Instead of using
Resourceto handle everything, I was able to leverage casting toClientResponseinstead ofStringwhich will return me back errors instead of just text when the response fails (for whatever reason).I need to make a JSON API call to an external source inside of my JAX-RS application. From my research, in order to do this I need to use RestClient(), which works. But only when I get a successful response back. It can not parse the body when the status code is anything other than 400. Here's an example of working code when I get a 200 status:
Old way:
New way: