responseentity-如何处理不同于200的状态码?

xmd2e60i  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(317)

我在一个restapi客户机上工作,我有一个类负责所有的请求指令,因为我将使用完全相同的提供者。我目前能够发送,接收和处理的答复,只要他们是200确定。但是,一旦我开始接收到与之不同的内容,就会出现错误,程序就会停止运行。

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方法的对象能够处理正结果和负结果(advancesDslrResponse.class或apiexception.class)

mkshixfv

mkshixfv1#

你可以抓住 RestClientResponseException 并使用 getResponseBodyAsString() 从例外中。然后你可以用jackson把它Map到一个物体上。
我就是这样处理的,也许还有别的办法。
关于最后一个问题,我想您需要在response对象中添加另一个字段,以便能够从异常传递json。另一种方法是使用另一个表达式重新抛出异常并稍后捕获,或者根本不重新抛出异常并稍后捕获。

相关问题