【问题标题】:How to parse reponse body from REST API when response code is 4xx in javajava中响应代码为4xx时如何从REST API解析响应正文
【发布时间】:2020-01-12 23:58:21
【问题描述】:

我正在尝试使用 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 错误

【问题讨论】:

    标签: java rest http oauth-2.0


    【解决方案1】:

    你可以在 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;
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2018-02-23
      • 2023-04-06
      • 2011-07-17
      相关资源
      最近更新 更多