[英]How to parse reponse body from REST API when response code is 4xx in java
I am trying to access REST API using OauthClient我正在尝试使用 OauthClient 访问 REST API
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.当我使用 Postman 执行调用时,api 调用会返回一个响应正文,但是当我使用上面的这段代码时,它抛出异常并且我看不到响应正文,以便对其进行解析。
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是否可以解析响应正文的 4xx 错误
You can build your response object in catch block and return like您可以在 catch 块中构建您的响应对象并返回
} catch (Exception ex) {
return Response.status(Response.Status.BAD_REQUEST).entity(new PresenterClass(ex.getMessage())).build();
}
Using Presenter class constructor使用 Presenter 类构造函数
Public PresenterClass(String errorMessage){
this.message = errorMessage;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.