码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • openfeign客户端A调用服务B,服务B抛出异常时,客户端A接收的几种情况


    openfeign客户端A调用服务B,服务B抛出异常时,客户端A接收的几种情况

    • 一、openfeign客户端调用远程服务时,远程服务抛出异常后,根据调用方法的返回类型的不同,会有不同的返回结果
      • 0 先看非openfeign调用的情况:controller直接抛出异常
      • 1 openfeign客户端A调用服务B:调用方法返回String类型
        • 结论 :openfeign调用时,如果方法返回类型为String类型时,则与非openfeign调用返回信息是一致的。
      • 2 openfeign客户端A调用服务B:调用方法返回 非 String类型
        • 结论 : openfeign客户端A调用服务B:调用方法返回 非 String类型时,报错信息为:“Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Integer] and content type [text/html;charset=UTF-8]” (这里以Integer为返回类型)
      • 3 服务B返回null值的情况
        • a 报错时,参考:
        • b 不报错,无话可说
        • c 待跟踪确认。。。

    一、openfeign客户端调用远程服务时,远程服务抛出异常后,根据调用方法的返回类型的不同,会有不同的返回结果

    0 先看非openfeign调用的情况:controller直接抛出异常

    controller层代码,其他一切使用默认,不做任何配置:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ClassForTest {
    	Logger logger = LoggerFactory.getLogger(this.getClass());
    
    	@Autowired
    	private XlServiceImpl xlServiceImpl;
    
    	@GetMapping("/dosth")
    	public String doSth() throws Exception {
    		throw new Exception("测试抛出异常!");
    //		String affect = "";
    //		affect = xlServiceImpl.doInsert();
    //		return affect;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    访问接口/dosth,调用controller方法:
    在这里插入图片描述

    1 openfeign客户端A调用服务B:调用方法返回String类型

    • openfeign客户端代码
      controller代码
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class ClassForTest {
    	Logger logger = LoggerFactory.getLogger(this.getClass());
    
    	@Autowired
    	private XlServiceImpl xlServiceImpl;
    
    	@GetMapping("/dosth")
    	public String doSth() throws Exception {
    		String affect = "";
    		affect = xlServiceImpl.doInsert();
    		return affect;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    serviceImpl层关键代码代码 : 使用openfeign的客户端--xlFeignClient进行调用,并且返回String类型

    	public String doInsert() throws Exception {
    		String ir = "";
    		try {
    			ir = xlFeignClient.invokeBusi();
    		} catch (Exception e) {
    			logger.error(e.getMessage());
    		}
    		return ir;
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    openfeign的客户端--xlFeignClient代码如下:

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PostMapping;
    
    @FeignClient(contextId = "xl20231108", name = "checklist-service")
    public interface XlFeignClient {
    	@PostMapping("/checklistservice/xl/test")
    	String invokeBusi();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 服务B的代码
      controller层代码:
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class XlContoller {
    
    	@Autowired
    	private com.omomcam.checklistservice.service.impl.XlBusiServiceImpl XlBusiServiceImpl;
    
    	@PostMapping("/checklistservice/xl/test")
    	public String invokeBusi() {
    		return XlBusiServiceImpl.test();
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    sericeImpl层代码:

    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    
    @Service
    @Transactional(rollbackFor = Exception.class)
    public class XlBusiServiceImpl {
    	
    	@Autowired
    	private XlBusiMapper xlBusiMapper;
    	
    	public String test() {
    		xlBusiMapper.insert1();
    		int x = 0;
    		int y = 3 / x ;
    		return "这是返回值!";
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 访问接口/dosth,调用controller方法,结果如下:
      在这里插入图片描述

    结论 :openfeign调用时,如果方法返回类型为String类型时,则与非openfeign调用返回信息是一致的。

    解释:服务抛出了异常信息,返回的如上图‘Whitelabel Error Page....’的信息,其格式为text/html,而String又可以正常接收这种格式的信息!

    2 openfeign客户端A调用服务B:调用方法返回 非 String类型

    将返回类型改为自定义类型或者Integer获取其他非String类型,这里以Integer为例:
    openfeign客户端修改返回类型为Integer,其他的对应修改即可

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PostMapping;
    
    @FeignClient(contextId = "xl20231108", name = "checklist-service")
    public interface XlFeignClient {
    	@PostMapping("/checklistservice/xl/test")
    	Integer invokeBusi();
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    访问接口/dosth,调用controller方法,结果如下:
    在这里插入图片描述
    解释: 在调用服务B时返回的数据为 text/html ,而方法接收类型为Integer,二者不一致,并且无法正确转换, 所以报了如上错误。

    结论 : openfeign客户端A调用服务B:调用方法返回 非 String类型时,报错信息为:“Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Integer] and content type [text/html;charset=UTF-8]” (这里以Integer为返回类型)

    3 服务B返回null值的情况

    服务端B返回null值,客户端有时报错!有时不报错!需跟踪确认—2023年11月10日10:29:16

    a 报错时,参考:

    OpenFeign客户端调用,服务端查询结果为null并返回给feign客户端,引发客户端报错

    b 不报错,无话可说

    c 待跟踪确认。。。

  • 相关阅读:
    Mac版Jmeter安装与使用&模拟分布式环境
    Java-Quartz实现定时任务(SpringBoot整合quartz)
    MOS管米勒效应
    标记肽Suc-AAPI-pNA、72682-77-0
    Flask 学习-37.Flask-RESTful 序列化输出fields 字段设置
    Rust编程中的线程间通信
    MySQL数据库20G数据迁移至其他服务器的MySQL库或者云MySQL库
    OceanBase 笔记
    【力客热题HOT100】-【040】101 对称二叉树
    思科防火墙IPsec配置-野蛮模式方式(基于9.9版本)
  • 原文地址:https://blog.csdn.net/qq_29025955/article/details/134294967
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号