在java中,当响应代码为4xx时,如何从RESTAPI解析响应体
我正在尝试使用OauthClient访问RESTAPI在java中,当响应代码为4xx时,如何从RESTAPI解析响应体,java,rest,http,oauth-2.0,Java,Rest,Http,Oauth 2.0,我正在尝试使用OauthClient访问RESTAPI try { OAuthClient client = new OAuthClient(new URLConnectionClient()); OAuthResourceResponse response = client.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class); } catch (Exception ex) {
try {
OAuthClient client = new OAuthClient(new URLConnectionClient());
OAuthResourceResponse response = client.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
当我使用Postman执行调用时,api调用返回一个响应体,但当我使用上面的代码时,它抛出异常,我无法看到响应体,以便对其进行解析
例外情况如下:
org.apache.oltu.oauth2.common.exception.OAuthSystemException: java.io.IOException: Server returned HTTP response code: 409 for URL:
是否可以解析4xx错误的响应体您可以在catch块中构建响应对象,然后像
} catch (Exception ex) {
return Response.status(Response.Status.BAD_REQUEST).entity(new PresenterClass(ex.getMessage())).build();
}
使用Presenter类构造函数
Public PresenterClass(String errorMessage){
this.message = errorMessage;
}