简体   繁体   中英

How to parse reponse body from REST API when response code is 4xx in java

I am trying to access REST API using OauthClient

try {
        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthResourceResponse response = client.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
} catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
}

The api call returns a response body when I execute the call using Postman, but when I use this code above, it throws exception and I can not see the response body, in order to parse it.

Here is the exception:

org.apache.oltu.oauth2.common.exception.OAuthSystemException: java.io.IOException: Server returned HTTP response code: 409 for URL: 

Is it possible to parse the response body for 4xx errors

You can build your response object in catch block and return like

} catch (Exception ex) {
    return Response.status(Response.Status.BAD_REQUEST).entity(new PresenterClass(ex.getMessage())).build();
}

Using Presenter class constructor

 Public  PresenterClass(String errorMessage){
    this.message = errorMessage;

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM