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

文章讨论了OpenFeign客户端A调用服务B时,服务B抛出异常的不同处理方式,重点关注了根据返回类型(如String和非String)以及服务返回null值时的响应结果和错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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;
	}
}

访问接口/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;
	}
}

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

	public String doInsert() throws Exception {
		String ir = "";
		try {
			ir = xlFeignClient.invokeBusi();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return ir;
	}

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();
}
  • 服务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();
	}
}

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 "这是返回值!";
	}
	
}

  • 访问接口/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();
	
}

访问接口/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 待跟踪确认。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值