赞
踩
spring 中 http调用
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, new HttpEntity<>(req, headers), String.class);
resp 会返回3个值
status/heard/body
分2种情况的异常
1.status 代表了通信异常, 也是我们常说的 http 状态码
只能是纯数字
200-成功, 404-不能访问的资源, 500-服务器异常
比较坑的是, 有些服务会, 把业务异常和这个进行合并, 或者篡改
2.body中的业务异常, 这个是在消息体内, 通过json解析后提取
由于是自定义的, 所以有多种表现形式
纯数字的字符串, 纯数字, 英文+数字组合
==========
变种1: 更复杂一点的情况是, 调用的是一个中转服务, 会嵌套消息体/使用状态码+业务码
变种2: 人为造就的 情况, 情况1+情况2, 在同一个服务上混乱的出现
比如:
接口A, http status 600-是通信正常/ 业务码 0-业务正常
接口B, http status 200-是通信正常/ 业务码 200-业务正常
此时, 就只能针对接口做分类, 单独处理
=============
现实的情况多种多样
处理的逻辑 就简单分2类即可
BusinessException 业务异常-运行时异常
HttpInternalException http通信内部异常-检查时异常
然后根据对应的情况, 抛出异常信息即可
举例
http状态码 200
返回的json值
{
"code": 0,
"message": "成功"
}
-
- ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, new HttpEntity<>(omsReq, headers),
- String.class);
- log.info("| REST | OMS | orderCancel |, response body {}", responseEntity.getBody());
- try {
- // json值转换为 oms服务的通用响应对象
- OMSResponseDto resp = json2Dto(responseEntity.getBody());
-
- // 前面json工具的转换后 resp不会是null, 但值有可能是null
- if (resp.getCode() != null && resp.getCode() != 0) {
- throw new BusinessException(resp.getMessage());
- }
- } catch (BusinessException ex) {
- throw new BusinessException("| REST | OMS | orderCancel | Exception | message: " + ex.getMessage());
- } catch (Exception e) {
- throw new HttpInternalException("| REST | OMS | orderCancel | Exception | Unknown internal error", e);
- }

1.保留了业务异常信息的情况
2.区分了http调用的各种网络通信问题(情况不多见, 偶发时查日志就行)
总结: 对测试人员友好, 能够快速知道报错原因, 降低了查服务器日志的频率
上述代码, 也可以一次性转掉, 这种就是没有检查异常, 使用时比较省事
- try {
- // json值转换为 oms服务的通用响应对象
- OMSResponseDto resp = json2Dto(responseEntity.getBody());
-
- // 前面json工具的转换后 resp不会是null, 但值有可能是null
- if (resp.getCode() != null && resp.getCode() != 0) {
- throw new BusinessException("| REST | OMS | orderCancel | Exception | message: " + resp.getMessage());
- }
- } catch (Exception e) {
- if (e instanceof BusinessException) {
- throw (BusinessException) e;
- }
- throw new BusinessException("| REST | OMS | orderCancel | Exception | Unknown internal error", e);
- }
ps: 遇到自定义的httpCode值, 则可以捕获 UnknownHttpStatusCodeException 异常
=======
spring 项目 RestClientException 是基类
HttpClientErrorException extends HttpStatusCodeException 处理4xx异常 HttpServerErrorException extends HttpStatusCodeException 处理5xx异常 UnknownHttpStatusCodeException 比如各种自定义的 httpCode值 599 600等
java 的2个异常都属于 IOException
HttpConnectTimeoutException
HttpTimeoutException
- https://www.codenong.com/8647e8891954a88373be/
-
-
- 异常列表
-
- 找不到HTTP 404的
- 引发异常:org.springframework.web.client.HttpClientErrorException
- HttpClientErrorException.NotFound是子类
-
- 对于HTTP 500内部服务器错误
- 引发异常:org.springframework.web.client.HttpServerErrorException
- HttpServerErrorException.InternalServerError是子类
-
- 对于未知状态码
- 发生异常:org.springframework.web.client.UnknownHttpStatusCodeException
-
- 不是URL字符串
- 引发异常:java.lang.IllegalArgumentException
- 如果无法将其解析为URL字符串
- 原因异常:java.net.URISyntaxException
-
- URL的方案名称未知
- 发生的异常:org.springframework.web.client.ResourceAccessException
- 原因异常:java.net.MalformedURLException
-
- 无法从主机名中减去IP地址
- 发生的异常:org.springframework.web.client.ResourceAccessException
- 原因异常:java.net.UnknownHostException
-
- 访问未启动Web服务器的端口时(拒绝连接)
- 发生的异常:org.springframework.web.client.ResourceAccessException
- 原因异常:java.net.ConnectException
-
- 对于不受信任的SSL服务器证书(自签名证书,oleore证书)
- 发生的异常:org.springframework.web.client.ResourceAccessException
- 原因异常:javax.net.ssl.SSLHandshakeException
-
- 如果连接超时(例如,机器不存在的IP地址)
- 发生的异常:org.springframework.web.client.ResourceAccessException
- 原因异常:java.net.SocketTimeoutException
-
- 读取超时
- 发生的异常:org.springframework.web.client.ResourceAccessException
- 原因异常:java.net.SocketTimeoutException

===================
ResponseEntity 被用于RestTemplate和Controller层方法
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。