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

相关推荐
宇先生本人号几秒前
别再用“AI 说完成了”:一套可复算的 Agent 验收流水线
后端
张明君644512 分钟前
在 Astro SSR 项目中实现服务启动前初始化
前端·后端
用户9338104348455 分钟前
MyBatis-Plus LambdaQueryWrapper 隐式转换踩坑:不知不觉索引失效
java
用户37215742613512 分钟前
Java 在 PDF 中查找关键词:获取页码和坐标,并高亮匹配结果
java
用户37215742613519 分钟前
Java 删除 Excel 重复行:整行去重、按单列和多列去重
后端
Forget the Dream38 分钟前
Spring AI 2.0 系列(三):ChatClient 与 ChatModel:Spring AI 2.0 的两层职责
java·人工智能·spring·springai
AI入职记1 小时前
RAG 一个请求吃掉 8 个核,chat 只要 0.16 个
后端·ai编程
aurora_zjj1 小时前
Nginx $request_length 为什么会变成负数?一次流量统计异常的源码排查
后端
旋律4141 小时前
布隆过滤器(Bloom Filter)
后端
站大爷IP2 小时前
Python的构造方法把我坑惨了,原来__new__和__init__的区别这么大
后端