Spring Cloud Alibaba 之 “Feign多参数构造”

在上一篇文章整合好了Feign,现在来总结以下Feign调用多参数方法的使用。

GET方式:

Spring Cloud为Feign支持了Spring Mvc注解的。如果请求的是localhost:8083/test?id=1&name=coco,那么如果我们这样写(User实体类有这二个属性)

复制代码
@FeignClient("people-center")
public interface UserFeignClient {
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User test(User user);
}

这样其实是会报错的,因为即使我们指定了GET,但是Feign还是会以POST方式请求,所以我们需要修改成如下这样的

复制代码
@FeignClient("people-center")
public interface UserFeignClient {
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User test(@SpringQueryMap User user);
}

或者是

复制代码
@FeignClient("people-center")
public interface UserFeignClient {
  @RequestMapping(value = "/get", method = RequestMethod.GET)
  public User test(@RequestParam("id") Long id,@RequestParam("name") String name);
复制代码
或者是
复制代码
 @FeignClient("people-center")
  public interface UserFeignClient {
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public User test(@RequestParam Map<String, Object> map);
  }

推荐使用第二种,因为比较直观

POST请求

代码解读

复制代码
    @FeignClient("people-center")
    public interface UserFeignClient {
      @RequestMapping(value = "/post", method = RequestMethod.POST)
      public User test(@RequestBody User user);
    }

POST请求就很简单了,直接按照上面方式写就行了

相关推荐
文丑颜不良啊12 分钟前
LeetCode2441~2460题目解析
java·leetcode
喵个咪33 分钟前
GoWind Admin 拆解:BI 大屏设计器——schema 即 JSON、resolver 注入与三张快照表的口径谱系
大数据·后端·go
喵个咪36 分钟前
GoWind Admin 拆解:AI 能力层——工具循环、pgvector 知识库与机器凭证
人工智能·后端·agent
Hilaku44 分钟前
你们公司有 35 岁以上的前端吗?
前端·javascript·程序员
泡海椒1 小时前
jquick-pdf 柱状图(JBar)实战:Java 生成政务项目审批数量趋势 PDF
后端
AI前端工程狮1 小时前
服务端已经死了,客户端却报"超时"
人工智能·程序员
阿屿1 小时前
从零手搓 AI 伴侣(二):好感度通胀危机,与它的「货币政策」
java·人工智能
灿飞1 小时前
Kotlin与Java虚拟机十一:类与属性:构造顺序、backing field 与显式 backing field
java·后端·kotlin
终码一生1 小时前
IntelliJ IDEA 2026.3 发布:智能体住进 IDE
后端
Gopher_HBo1 小时前
profbuf.go —— 单写单读无锁环形缓冲
后端