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

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

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

  1. Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2. 2021-02-09 15:17:09.359 ERROR 24504 --- [ main] o.s.boot.SpringApplication : Application run failed
  3. java.lang.IllegalStateException: Failed to execute CommandLineRunner
  4. at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) ~[spring-boot-2.4.0.jar:2.4.0]
  5. at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) ~[spring-boot-2.4.0.jar:2.4.0]
  6. at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.0.jar:2.4.0]
  7. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) ~[spring-boot-2.4.0.jar:2.4.0]
  8. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) ~[spring-boot-2.4.0.jar:2.4.0]
  9. at com.infobipcommunity.InfobipApisApplication.main(InfobipApisApplication.java:38) ~[classes/:na]
  10. Caused by: org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{
  11. "requestError": {
  12. "serviceException": {
  13. "messageId": "BAD_REQUEST",
  14. "text": "[from : may not be null, to : size must be between 1 and 2147483647]"
  15. }
  16. }
  17. }]
  18. at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101) ~[spring-web-5.3.1.jar:5.3.1]
  19. at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:186) ~[spring-web-5.3.1.jar:5.3.1]
  20. at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:125) ~[spring-web-5.3.1.jar:5.3.1]
  21. at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.3.1.jar:5.3.1]
  22. at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:818) ~[spring-web-5.3.1.jar:5.3.1]
  23. at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776) ~[spring-web-5.3.1.jar:5.3.1]
  24. at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:710) ~[spring-web-5.3.1.jar:5.3.1]
  25. at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:601) ~[spring-web-5.3.1.jar:5.3.1]
  26. at com.infobipcommunity.configuration.Request.requestExecutor(Request.java:72) ~[classes/:na]
  27. at com.infobipcommunity.controller.RequestManagerSms.getDeliveryReport(RequestManagerSms.java:33) ~[classes/:na]
  28. at com.infobipcommunity.InfobipApisApplication.run(InfobipApisApplication.java:62) ~[classes/:na]
  29. at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) ~[spring-boot-2.4.0.jar:2.4.0]
  30. ... 5 common frames omitted

根据证据你可以看到我得到的答复正文,我能够打印出来,不幸的是,我不能传递给下一个类。
这是我用来获取数据的方法:

  1. public AdvanceSmsDlrResponse getDeliveryReport() {
  2. AdvanceSmsDlrResponse dlrResponse = new AdvanceSmsDlrResponse();
  3. try {
  4. dlrResponse = objectMapper.readValue((String) requestExecutor(null, "/sms/1/reports", HttpMethod.GET,
  5. "com.infobipcommunity.models.responses.sms.getDeliveryReport.AdvanceSmsDlrResponse"), AdvanceSmsDlrResponse.class);
  6. return null;
  7. } catch (JsonProcessingException e) {
  8. e.printStackTrace();
  9. return new AdvanceSmsDlrResponse();
  10. }
  11. }

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

  1. public Object requestExecutor(Object requestBody, String requestPath, HttpMethod httpMethod, String className) {
  2. entity = new HttpEntity<>(requestBody, httpHeaders);
  3. response = requestRestTemplate.exchange(getUrlhost() + requestPath, httpMethod, entity, className.getClass());
  4. LOGGER.info("\n" + response.toString());
  5. return response.getBody();
  6. }

所以我的问题是,这意味着如何处理?
另外,我想问是否有方法可以确保使用getdeliveryreport方法的对象能够处理正结果和负结果(advancesDslrResponse.class或apiexception.class)

mkshixfv

mkshixfv1#

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

相关问题