今天遇到使用Feign调用微服务,传递参数时遇到几个问题

1.无参数

以GET方式请求

服务提供者

@RequestMapping("/hello")
public String Hello(){
return "hello,provider";
}

服务消费者

@GetMapping("/hello")
String hello();

2.单个参数

(1)GET——@PathVariable

服务提供者

@GetMapping("/test/{name}")
public String test(@PathVariable String name){
return "hello,"+name;
}

服务消费者

@GetMapping("/test/{name}")
String test(@PathVariable("name") String name);

(2)GET——@RequestParam

服务提供者

@RequestMapping("/test")
public String test(String name){return "hello,"+name;
}

服务消费者

@RequestMapping("/test")
String test(@RequestParam String name);

会遇到报错

RequestParam.value() was empty on parameter 0

解决方法:

  加上注解的描述,修改为

@RequestMapping("/test")
String test(@RequestParam("name") String name);

(3)POST

@RequestBody

不需要注解的描述

@RequestMapping("/test")
String test(@RequestBody String name);

注:

  参数前使用了@RequestBody注解的,都以POST方式消费服务

  @RequestBody注解的参数,需要POST方式才能传递数据

2.Feign多参数的问题

(1)GET——@PathVariable

服务提供者

@GetMapping("/test/{name}/{xyz}")
public String test(@PathVariable String name,@PathVariable String xyz){
return "hello,"+name+","+xyz;
}

服务消费者

@GetMapping("/test/{name}/{xyz}")
String test(@PathVariable("name") String name,@PathVariable("xyz") String xyz);

(1)GET——@RequestParam

服务提供者

@RequestMapping("/test")
public String test(String name,Integer type){
if(type==1){
return "hello,"+name;
}else{
return "hello,provider-"+name;
}
}

服务消费者

@RequestMapping("/test")
String test(String name, Integer type);

会遇到报错Method has too many Body parameters

说明:

  如果服务消费者传过来参数时,全都用的是@RequestParam的话,那么服务提供者的Controller中对应参数前可以写@RequestParam,也可以不写

  服务消费者feign调用时,在所有参数前加上@RequestParam注解

正确的写法

@RequestMapping("/test")
String test(@RequestParam("name") String name, @RequestParam("type") Integer type);

(2)POST

如果接收方不变

服务消费者

@RequestMapping("/test")
String test(@RequestBody String name, @RequestBody Integer type);

会遇到报错Method has too many Body parameters

服务消费者为

@RequestMapping("/test")
String test(@RequestBody String name, @RequestParam("type") Integer type);

name的值会为null

说明:

  如果服务消费者传过来参数,有@RequestBody的话,那么服务提供者的Controller中对应参数前必须要写@RequestBody

正确的写法

服务提供者

@RequestMapping("/test")
public String test(@RequestBody String name, Integer type){
if(type==1){
return "hello,"+name;
}else{
return "hello,provider-"+name;
}
}

服务消费者正确的写法

@RequestMapping("/test")
String test(@RequestBody String name, @RequestParam("type") Integer type);

可以接收到参数

总结:

  请求参数前加上注解@PathVariable、@RequestParam或@RequestBody修饰

  可以有多个@RequestParam,但只能有不超过一个@RequestBody

  使用@RequestParam注解时必须要在后面加上参数名

  @RequestBody用来修饰对象,但是既有@RequestBody也有@RequestParam,那么参数就要放在请求的url中,@RequestBody修饰的就要放在提交对象中

  当参数比较复杂时,feign即使声明为get请求也会强行使用post请求

SpringCloud Feign 参数问题的更多相关文章

  1. SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法(2)

    书接上文. 上文中描述了如何在 SpringCloud+Feign环境下上传文件与form-data同时存在的解决办法,实践证明基本可行,但却会引入其他问题. 主要导致的后果是: 1. 无法与普通Fe ...

  2. SpringCloud Feign 之 Fallback初体验

    SpringCloud Feign 之 Fallback初体验 在微服务框架SpringCloud中,Feign是其中非常重要且常用的组件.Feign是声明式,模板化的HTTP客户端,可以帮助我们更方 ...

  3. SpringCloud Feign通过FallbackFactory显示异常信息

    SpringCloud Feign可以进行服务消费,而且内置了Hystrix,能够进行熔断. Feign可以通过fallback指定熔断回调的类.代码示例及讲解可见: https://www.cnbl ...

  4. SpringCloud Eureka参数配置项详解

    SpringCloud Eureka参数配置项详解(转) Eureka涉及到的参数配置项数量众多,它的很多功能都是通过参数配置来实现的,了解这些参数的含义有助于我们更好的应用Eureka的各种功能,下 ...

  5. SpringCloud Feign 之 超时重试次数探究

    SpringCloud Feign 之 超时重试次数探究 上篇文章,我们对Feign的fallback有一个初步的体验,在这里我们回顾一下,Fallback主要是用来解决依赖的服务不可用或者调用服务失 ...

  6. springCloud feign使用/优化总结

    基于springCloud Dalston.SR3版本 1.当接口参数是多个的时候 需要指定@RequestParam 中的value来明确一下. /** * 用户互扫 * @param uid 被扫 ...

  7. SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法

    最近项目转型使用SpringCloud框架下的微服务架构,各微服务之间使用Feign进行调用.期间,发现若被调用方法涉及到文件上传且仅存在单个文件时,一切正常,代码片段如下: @RequestMapp ...

  8. SpringCloud Feign重试详解

    摘要: 今天在生产环境发生了数据库进程卡死的现象,除了sql因为全量更新,没加索引的原因,最主要还是我们的接口的服务器端接口出现问题了.忽视了更新接口的幂等性,以及调用方feign client的重试 ...

  9. SpringCloud Feign 常用代码

    服务提供者 服务提供者,是位于其他项目里面的. 服务提供者提供的方法,在Controller层里面,有可访问的Url. @Controller @RequestMapping("/order ...

随机推荐

  1. 如何取消 SqlDataAdapter.Fill() 的执行(转载)

    问 Scenario: We have a DataGridView which is attached to DataAdapter (datatable), we load the data in ...

  2. LinuxShell——正则表达式

    LinuxShell——正则表达式 摘要:本文主要学习了Shell中的正则表达式. 简介 含义 正则表达式,也称作正规表示法,是用于描述字符排列和匹配模式的一种语法规则,它主要用于字符串的模式分割.匹 ...

  3. JavaScript 字符串(String) 大全

    JavaScript字符串存储一系列字符,如“John Doe”.字符串可以是双引号或单引号内的任何文本: <!DOCTYPE html> <html> <meta ch ...

  4. 汇编指令之JMP,CALL,RET(修改EIP的值!!!)

    简单介绍了,JMP指令按市面上的意思来说是跳转到指定地址,但我这里不这么说,JMP, CALL, RET三个指令均为修改EIP值的指令,EAX, ECX, EBX, EDX, ESP, EBP, ES ...

  5. 解决jmeter进行分布式测试,远程机器来运行脚本,在察看结果树中的响应数据项为空白

    下面为大家提供一个解决办法: 第一步:打开主控机的jmeter--bin目录下的jmeter.properties文件 第二步:查找到mode=Standard 项 第三步:将其前边的注释去掉,然后保 ...

  6. Spark(4)

    Spark Core官网学习回顾 Speed disk 10x memory 100x Easy code interactive shell Unified Stack Batch Streamin ...

  7. nrm : 无法加载文件 C:\Users\TANG\AppData\Roaming\npm\nrm.ps1,因为在此系统上禁止运行脚本。

    1.win+s 搜索powershell 以管理身份运行 2.使用set-ExecutionPolicy RemoteSigned命令将计算机上的执行策略更改为 RemoteSigned,输入Y确定 ...

  8. 《高性能 Go 代码工坊》中译

    深入研究 Go 应用性能提升的英语系列文章,这里是中译 https://www.yuque.com/ksco/uiondt

  9. Centos7 下安装Redis4.0.6

    一.安装redis 第一步:下载redis安装包 wget http://download.redis.io/releases/redis-4.0.6.tar.gz [root@iZwz991stxd ...

  10. SSM整合开发——基于SSM的OA系统

    一.课程介绍 链接: https://pan.baidu.com/s/18B-lWfOUnKZPvuVEHY_NmQ 提取码: ky7t 复制这段内容后打开百度网盘手机App,操作更方便哦 需要 to ...