SpringBoot工程下使用OpenFeign的坑及解决
作者:小林June 时间:2022-08-24 18:00:38
一、前言
在SpringBoot工程(注意不是SpringCloud)下使OpenFeign的大坑。为什么不用SpringCloud中的Feign呢?
首先我的项目比较简单(目前只有login与业务模块)所以暂时不去引入分布式的架构,但两个服务之间存在一些联系因此需要接口调用接口(实现该操作方式很多我选择了OpenFeign,踩坑之路从此开始。。。)。
二、具体的坑
使用OpenFeign我是直接参考官方的demo,官方的例子写的简洁明了直接套用到自己的工程中即可,自己也可以做相应的封装再调用但demo中使用到了一个feign的核心注解@RequestLine,问题就是出在该注解上。
此时你去做调试如果使用的是GET请求,被请求的接口则会收到POST的请求然后A接口(请求方)就报500的错误,B接口(被请求方)则提示接口不支持POST请求(不支持POST请求是非常正常的,因为B接口定义的method是GET方法)。
以下是我的代码片段:
自定义UserFeign接口
public interface UserFeign {
/**
* 根据userId获取用户信息
* @param userId
* @return
*/
@RequestLine("GET /user/getUserById?id={id}")
Result getUserById(@Param("id") String userId);
}
调用UserFeign接口
@Service
public class UserService{
/**
* 通过OpenFegin实现接口调用接口
* @param userId
* @return
*/
public Result getUserByIdWith(String userId) {
UserFeign userInfo = Feign.builder()
.decoder(new JacksonDecoder())
.target(UserFeign.class, "http://localhost:8080");
Result res = userInfo.getUserById(userId);
return res;
}
}
A接口 (请求方)
@RequestMapping(value = "/test", method = RequestMethod.GET)
public Result test() {
String id = "ad545461300a";
return userService.getUserByIdWith(id);
}
B接口 (被请求方)
@RequestMapping(value = "/getUserById", method = RequestMethod.GET)
public Result getUserByUserId(@RequestParam(required = true) String id){
if ("".equals(id)) {
throw new BusinessException(400, "userId不能为空!");
}
Users info = usersService.getUserById(id);
if (info == null) {
throw new BusinessException(404, "userId有误!");
}
return ResultUntil.success(info);
}
以上代码我做了一些小调整,将调用UesrFeign接口中的方法封装在UserService中并且使用了@service这样我就可以在其它地方直接注入UserService然后调用其中方法。
你会觉得以上代码跟官网的demo没啥区别但官方文档中并没有说明使用@RequestLine注解需要进行配置(事实上需要进行配置的)。
配置代码如下:
@Bean
public Contract useFeignAnnotations() {
return new Contract.Default();
}
完成以上的配置就可以进行正常的调用了,该问题困扰我好几天了今天终于解决了(真不容易),希望该文章没有白写。
最后感谢这篇文章让我在放弃的时候找到了思路。(调试中遇到的细节问题就不在此进行赘述了)
来源:https://blog.csdn.net/feiyst/article/details/88677223
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用C#调用百度地图并实现坐标点的设置以及读取示例
![](https://img.aspxhome.com/file/2023/5/87015_0s.png)
Android SearchView搜索控件使用方法详解
![](https://img.aspxhome.com/file/2023/7/103927_0s.gif)
Unity实现跑马灯抽奖效果
![](https://img.aspxhome.com/file/2023/4/67054_0s.jpg)
Springboot日志开启SLF4J过程解析
Struts2+Hibernate实现数据分页的方法
![](https://img.aspxhome.com/file/2023/8/89578_0s.jpg)
Java 处理超大数类型之BigInteger案例详解
LINQ排序操作符用法
![](https://img.aspxhome.com/file/2023/2/89902_0s.jpg)
SpringSecurity认证流程详解
![](https://img.aspxhome.com/file/2023/8/81988_0s.jpg)
Java实现小型图书馆管理系统
![](https://img.aspxhome.com/file/2023/8/64878_0s.jpg)
c#中实现图片灰度化技术详解
![](https://img.aspxhome.com/file/2023/3/88373_0s.png)
C#生成随机验证码代码分享
C#使用foreach循环遍历数组完整实例
Android SQLite数据库连接实现登录功能
![](https://img.aspxhome.com/file/2023/1/109181_0s.jpg)
一步步教你如何使用Java实现WebSocket
![](https://img.aspxhome.com/file/2023/1/77971_0s.png)
java编程多线程并发处理实例解析
Java定时器Timer使用方法详解
![](https://img.aspxhome.com/file/2023/8/76528_0s.png)
java单点登录(SSO)的实现
![](https://img.aspxhome.com/file/2023/5/92645_0s.png)