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方法:
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();
}
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 "这是返回值!";
}
}
解释:服务抛出了异常信息,返回的如上图‘Whitelabel Error Page....’的信息,其格式为text/html,而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,二者不一致,并且无法正确转换, 所以报了如上错误。
服务端B返回null值,客户端有时报错!有时不报错!需跟踪确认—2023年11月10日10:29:16
OpenFeign客户端调用,服务端查询结果为null并返回给feign客户端,引发客户端报错