![](/img/trans.png)
[英]How to handle HTTP response with a 4xx/5xx status code, Java servlet/HttpURLConnection
[英]How to parse reponse body from REST API when response code is 4xx in java
我正在尝试使用 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;
}
当我使用 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.