简体   繁体   English

java - 当响应代码为 4xx 时,如何从 REST API 解析响应正文

[英]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.

相关问题 如何使用4xx / 5xx状态代码,Java servlet / HttpURLConnection处理HTTP响应 - How to handle HTTP response with a 4xx/5xx status code, Java servlet/HttpURLConnection 解析 try catch 块中的异常以分配响应代码 - 2xx、4xx 或 5xx - Parse exception in try catch block to assign a response code - 2xx, 4xx or 5xx Java Spring Boot 框架 - WebClient 记录 4xx/5xx 主体响应 - Java Spring Boot framework - WebClient logging 4xx/5xx body response Spring WebClient - 如何在出现 HTTP 错误(4xx、5xx)的情况下访问响应正文? - Spring WebClient - how to access response body in case of HTTP errors (4xx, 5xx)? 使用原生 Java 检查 4xx Http 代码 - check for 4xx Http Code with native Java 4XX 和 5XX 错误时如何在 Java 中获取 json 响应 - How to get json response in Java in case of 4XX and 5XX error 防止Tomcat在HTTP错误状态4xx或5xx上干扰Jersey / JAX-RS 2响应主体 - Prevent Tomcat from interfering with Jersey/JAX-RS 2 Response Body on HTTP Error Status 4xx or 5xx 当我将Retrofit2.0与POST一起使用时,body为null,但响应代码为5xx? - When i use Retrofit2.0 with POST , body is null , but the reponse code is 5xx? 当状态代码为4xx时,GAE / J将Content-Type从JSON更改为HTML - GAE/J changes Content-Type from JSON to HTML when the status code is 4xx Jersey服务器,rest api:如何从响应正文中删除代码和类型? - Jersey server, rest api : How to remove the code and type from the response body?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM