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请求就很简单了,直接按照上面方式写就行了

相关推荐
天天被压力9 分钟前
【A股分钟级高频数据完全手册 #07】北交所历史分钟K线接口:京市Pro版分时批量
java·人工智能·python
天天被压力15 分钟前
【A股分钟级高频数据完全手册 #08】股票日、分时数据接口对比:普通分时与Pro版差异实测
java·人工智能·python
量化分析码农26 分钟前
【Python量化波动率实战 01】波动率是怎么算的:年化、滚动与区间估计一次讲清
后端
特立独行的猫A29 分钟前
AI 时代,我为什么推荐用 cj-tauri 开发桌面应用
java·后端·python
金銀銅鐵32 分钟前
从一个类名到一张类图:用反射自动生成 PlantUML
java·后端·uml
小郎君65844 分钟前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·spring cloud
追逐时光者1 小时前
一款基于 .NET 开源、免费的数据库自治诊断平台
后端·.net
nice_lcj5201 小时前
Java 并发容器入门:ConcurrentHashMap、CopyOnWriteArrayList、阻塞队列
java·面试·并发编程·阻塞队列·concurrenthashmap·copyonwritearraylist
微小的鱼1 小时前
Linux的渲染与显示系统链路
android·java·linux
追逐时光者1 小时前
让 GitHub Copilot 更懂你的 .NET 代码风格:一份精简实用的 Copilot Instructions
后端·.net