ResponseEntity - 如何处理与 200 ok 不同的状态代码?

时间:2021-02-09 18:32:07

标签: java spring rest error-handling

我正在开发 REST API 客户端,并且我有一个类负责所有请求指令,因为为此我将使用完全相同的提供程序。我目前能够发送、接收和处理响应,只要它们是 200 OK。 然而,一旦我开始收到与它不同的任何东西,我就会出错并且我的程序停止运行。

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-09 15:17:09.359 ERROR 24504 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.0.jar:2.4.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.0.jar:2.4.0]
    at com.infobipcommunity.InfobipApisApplication.main(InfobipApisApplication.java:38) ~[classes/:na]
Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{
    "requestError": {
        "serviceException": {
            "messageId": "BAD_REQUEST",
            "text": "[from : may not be null, to : size must be between 1 and 2147483647]"
        }
    }
}]
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:818) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) ~[spring-web-5.3.1.jar:5.3.1]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:601) ~[spring-web-5.3.1.jar:5.3.1]
    at com.infobipcommunity.configuration.Request.requestExecutor(Request.java:72) ~[classes/:na]
    at com.infobipcommunity.controller.RequestManagerSms.getDeliveryReport(RequestManagerSms.java:33) ~[classes/:na]
    at com.infobipcommunity.InfobipApisApplication.run(InfobipApisApplication.java:62) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.0.jar:2.4.0]
    ... 5 common frames omitted

根据证据,您可以看到我正在获取响应正文,并且可以打印它,但不幸的是我无法将其传递给下一堂课。

这是我用来获取数据的方法:

public AdvanceSmsDlrResponse getDeliveryReport() {

        AdvanceSmsDlrResponse dlrResponse = new AdvanceSmsDlrResponse();
        try {
            dlrResponse = objectMapper.readValue((String) requestExecutor(null, "/sms/1/reports", HttpMethod.GET,
                    "com.infobipcommunity.models.responses.sms.getDeliveryReport.AdvanceSmsDlrResponse"), AdvanceSmsDlrResponse.class);
            return null;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return new AdvanceSmsDlrResponse();
        }
    }

在上一个片段中调用的泛型方法:

public Object requestExecutor(Object requestBody, String requestPath, HttpMethod httpMethod, String className) {

    entity = new HttpEntity<>(requestBody, httpHeaders);
        response = requestRestTemplate.exchange(getUrlhost() + requestPath, httpMethod, entity, className.getClass());
        LOGGER.info("\n" + response.toString());
        return response.getBody();

}

所以我的问题是,这意味着如何处理?

另外,我想问一下是否有一种方法可以确保使用 getDeliveryReport 方法的对象能够处理正面和负面结果(AdvanceSmsDlrResponse.class 或 ApiException.class)

1 个答案:

答案 0 :(得分:0)

您可以捕获 RestClientResponseException 并使用 getResponseBodyAsString() 从异常中获取结果。然后就可以用jackson把它映射到一个对象上。

我是这样处理的,也许还有其他方法。

关于最后一个问题,我想您需要在响应对象中添加另一个字段,以便能够从异常中传递 JSON。另一种方法是使用另一个 expcetion 重新抛出异常并稍后捕获,或者根本不重新抛出并稍后捕获。