已解决
openfeign客户端A调用服务B,服务B抛出异常时,客户端A接收的几种情况
来自网友在路上 191891提问 提问时间:2023-11-10 11:44:45阅读次数: 91
最佳答案 问答题库918位专家为你答疑解惑
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());@Autowiredprivate 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());@Autowiredprivate 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 {@Autowiredprivate 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 {@Autowiredprivate 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 待跟踪确认。。。
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"openfeign客户端A调用服务B,服务B抛出异常时,客户端A接收的几种情况":http://eshow365.cn/6-37204-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!