有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ResponseEntity如何处理不同于200 ok的状态代码?

我在一个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) 个答案

  1. # 1 楼答案

    您可以捕获RestClientResponseException,并使用异常中的getResponseBodyAsString()获得结果。然后可以使用jackson将其映射到一个对象

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

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