• SpringBoot 项目中 对http调用异常处理


    spring 中 http调用

    ResponseEntity 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": "成功"
    }
     

    1. ResponseEntity responseEntity = restTemplate.postForEntity(url, new HttpEntity<>(omsReq, headers),
    2. String.class);
    3. log.info("| REST | OMS | orderCancel |, response body {}", responseEntity.getBody());
    4. try {
    5. // json值转换为 oms服务的通用响应对象
    6. OMSResponseDto resp = json2Dto(responseEntity.getBody());
    7. // 前面json工具的转换后 resp不会是null, 但值有可能是null
    8. if (resp.getCode() != null && resp.getCode() != 0) {
    9. throw new BusinessException(resp.getMessage());
    10. }
    11. } catch (BusinessException ex) {
    12. throw new BusinessException("| REST | OMS | orderCancel | Exception | message: " + ex.getMessage());
    13. } catch (Exception e) {
    14. throw new HttpInternalException("| REST | OMS | orderCancel | Exception | Unknown internal error", e);
    15. }

    1.保留了业务异常信息的情况

    2.区分了http调用的各种网络通信问题(情况不多见, 偶发时查日志就行)

    总结: 对测试人员友好, 能够快速知道报错原因, 降低了查服务器日志的频率

    上述代码, 也可以一次性转掉, 这种就是没有检查异常, 使用时比较省事

    1. try {
    2. // json值转换为 oms服务的通用响应对象
    3. OMSResponseDto resp = json2Dto(responseEntity.getBody());
    4. // 前面json工具的转换后 resp不会是null, 但值有可能是null
    5. if (resp.getCode() != null && resp.getCode() != 0) {
    6. throw new BusinessException("| REST | OMS | orderCancel | Exception | message: " + resp.getMessage());
    7. }
    8. } catch (Exception e) {
    9. if (e instanceof BusinessException) {
    10. throw (BusinessException) e;
    11. }
    12. throw new BusinessException("| REST | OMS | orderCancel | Exception | Unknown internal error", e);
    13. }

    ps: 遇到自定义的httpCode值, 则可以捕获 UnknownHttpStatusCodeException 异常

    =======

    spring 项目  RestClientException 是基类

    HttpClientErrorException extends HttpStatusCodeException  处理4xx异常
    HttpServerErrorException extends HttpStatusCodeException  处理5xx异常
    UnknownHttpStatusCodeException   比如各种自定义的 httpCode值 599 600等
    

    java  的2个异常都属于 IOException

    HttpConnectTimeoutException
    HttpTimeoutException

    1. https://www.codenong.com/8647e8891954a88373be/
    2. 异常列表
    3. 找不到HTTP 404
    4. 引发异常:org.springframework.web.client.HttpClientErrorException
    5. HttpClientErrorException.NotFound是子类
    6. 对于HTTP 500内部服务器错误
    7. 引发异常:org.springframework.web.client.HttpServerErrorException
    8. HttpServerErrorException.InternalServerError是子类
    9. 对于未知状态码
    10. 发生异常:org.springframework.web.client.UnknownHttpStatusCodeException
    11. 不是URL字符串
    12. 引发异常:java.lang.IllegalArgumentException
    13. 如果无法将其解析为URL字符串
    14. 原因异常:java.net.URISyntaxException
    15. URL的方案名称未知
    16. 发生的异常:org.springframework.web.client.ResourceAccessException
    17. 原因异常:java.net.MalformedURLException
    18. 无法从主机名中减去IP地址
    19. 发生的异常:org.springframework.web.client.ResourceAccessException
    20. 原因异常:java.net.UnknownHostException
    21. 访问未启动Web服务器的端口时(拒绝连接)
    22. 发生的异常:org.springframework.web.client.ResourceAccessException
    23. 原因异常:java.net.ConnectException
    24. 对于不受信任的SSL服务器证书(自签名证书,oleore证书)
    25. 发生的异常:org.springframework.web.client.ResourceAccessException
    26. 原因异常:javax.net.ssl.SSLHandshakeException
    27. 如果连接超时(例如,机器不存在的IP地址)
    28. 发生的异常:org.springframework.web.client.ResourceAccessException
    29. 原因异常:java.net.SocketTimeoutException
    30. 读取超时
    31. 发生的异常:org.springframework.web.client.ResourceAccessException
    32. 原因异常:java.net.SocketTimeoutException
    ===================

    ResponseEntity 被用于RestTemplate和Controller层方法

    https://blog.csdn.net/qq_33819764/article/details/81985694

  • 相关阅读:
    CentOS 添加NTP服务器配置
    「Python循环结构」利用while循环求1~n的平方和
    记录一个删库跑路的技巧(如何快速删除数据库下面的所有表)
    Linux——(第六章)常用指令(二)
    Linux文本编辑器---vim详解
    工程水文学名词解释总结
    从零开始—【Mac系统】MacOS配置Java环境变量
    【HarmonyOS4学习笔记】《HarmonyOS4+NEXT星河版入门到企业级实战教程》课程学习笔记(三)
    使用pycharm远程连接到Linux服务器进行开发
    [微前端实战]---02架构基础知识
  • 原文地址:https://blog.csdn.net/zx1323/article/details/127914155