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

相关推荐
hey_sml8 分钟前
JAVA每日一学---CompletableFuture中thenApply与thenCompose区别详解
java·开发语言
Doraemomo10 分钟前
数据结构-环形链表
java·数据结构·链表
维克兜率天1 小时前
【维克】ARMA模型:AR与MA的完美结合
后端·restful
萧瑟余晖2 小时前
Java深入解析篇十七之SpringCloud
java·开发语言
是未才2 小时前
从输入 URL 到页面返回:DNS、路由、TLS 与 HTTP 完整链路
java·后端·计算机网络
上海安当技术2 小时前
半天接入:USBKey RESTful API + C 动态库,Web 和 C/S 两套集成路径实战
前端·后端·restful·集成·usbkey
ttod_qzstudio2 小时前
Java 常用语法极简通关(五):类与对象——字段、方法、构造器、this 与 static
java·开发语言·python
DevUI团队3 小时前
从“即兴创作”到“规格先行”,华为云码道(CodeArts)代码智能体持续深耕企业级规范驱动开发能力
前端·人工智能·后端
萧瑟余晖4 小时前
Java深入解析篇十七之Spring Security
java·开发语言·spring
weixin_431600444 小时前
NestJS 入门(3):Guard 如何挡住未登录请求?
前端·后端·学习·nest.js