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

相关推荐
0和1的搬运工4 分钟前
基于Java+SpringBoot+Vue+HTML5高校教师电子名片系统(源码+LW+调试文档+讲解等)/高校教师/电子名片/系统/教育科技/教育信息化/名片管理/电子身份/教师信息管理/校园信息化
spring cloud·tomcat·log4j·maven·intellij-idea·dubbo·java-consul
敲敲千反田5 分钟前
Spring 相关
java·后端·spring
@小柯555m12 分钟前
Java八股刷题
java·开发语言·八股
bzmK1DTbd18 分钟前
Java在人工智能:TensorFlow Java API的使用
java·人工智能·tensorflow
小碗羊肉22 分钟前
【JavaWeb | 第四篇】分层解耦
java·后端·servlet
EM-FF26 分钟前
idea快捷键
java·ide·intellij-idea
薪火铺子33 分钟前
Redis 分布式锁与 Redisson 原理深度解析
java·redis·分布式·后端
胡楚昊35 分钟前
BUU WEB之旅(1)
java·数据库·mybatis
牢七1 小时前
链条合集整理
java·开发语言
小雅痞1 小时前
[Java][Leetcode hard] 30. 串联所有单词的子串
java·leetcode