SpringCloud Feign 参数问题
今天遇到使用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 参数问题的更多相关文章
- SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法(2)
书接上文. 上文中描述了如何在 SpringCloud+Feign环境下上传文件与form-data同时存在的解决办法,实践证明基本可行,但却会引入其他问题. 主要导致的后果是: 1. 无法与普通Fe ...
- SpringCloud Feign 之 Fallback初体验
SpringCloud Feign 之 Fallback初体验 在微服务框架SpringCloud中,Feign是其中非常重要且常用的组件.Feign是声明式,模板化的HTTP客户端,可以帮助我们更方 ...
- SpringCloud Feign通过FallbackFactory显示异常信息
SpringCloud Feign可以进行服务消费,而且内置了Hystrix,能够进行熔断. Feign可以通过fallback指定熔断回调的类.代码示例及讲解可见: https://www.cnbl ...
- SpringCloud Eureka参数配置项详解
SpringCloud Eureka参数配置项详解(转) Eureka涉及到的参数配置项数量众多,它的很多功能都是通过参数配置来实现的,了解这些参数的含义有助于我们更好的应用Eureka的各种功能,下 ...
- SpringCloud Feign 之 超时重试次数探究
SpringCloud Feign 之 超时重试次数探究 上篇文章,我们对Feign的fallback有一个初步的体验,在这里我们回顾一下,Fallback主要是用来解决依赖的服务不可用或者调用服务失 ...
- springCloud feign使用/优化总结
基于springCloud Dalston.SR3版本 1.当接口参数是多个的时候 需要指定@RequestParam 中的value来明确一下. /** * 用户互扫 * @param uid 被扫 ...
- SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法
最近项目转型使用SpringCloud框架下的微服务架构,各微服务之间使用Feign进行调用.期间,发现若被调用方法涉及到文件上传且仅存在单个文件时,一切正常,代码片段如下: @RequestMapp ...
- SpringCloud Feign重试详解
摘要: 今天在生产环境发生了数据库进程卡死的现象,除了sql因为全量更新,没加索引的原因,最主要还是我们的接口的服务器端接口出现问题了.忽视了更新接口的幂等性,以及调用方feign client的重试 ...
- SpringCloud Feign 常用代码
服务提供者 服务提供者,是位于其他项目里面的. 服务提供者提供的方法,在Controller层里面,有可访问的Url. @Controller @RequestMapping("/order ...
随机推荐
- Linux SELinux 使用操作
Linux SELinux 使用操作 # 修改 SELinux 启动模式.临时生效 命令:setenforce [0|1] 0:转成 permissive 宽容模式: 1:转成 Enforcing 强 ...
- linux中查找包含指定内容的文件
Linux查找文件内容的常用方法 ##文件名+内容 grep -r "查询内容" 文件目录 ##只显示包含内容的文件名 grep -r -l "查询内容" 文件 ...
- tsconfig.json配置项详解
{ "compilerOptions": { "allowUnreachableCode": true, // 不报告执行不到的代码错误. "allo ...
- C++中Lambda表达式转化为函数指针
// ----------------------------------------------------------- auto combineCallbackLambda = [](GLdou ...
- JMM和Volatile底层原理分析
JMM和volatile分析 1.JMM:Java Memory Model,java线程内存模型 JMM:它是一个抽象的概念,描述的是线程和内存间的通信,java线程内存模型和CPU缓存模型类似,它 ...
- vue-router 在项目中的使用
一.下载vue-router npm install vue-router --save 二.编码 1.在项目中新建文件夹 router/index.js /* * 路由对象模块 * */ impor ...
- ios路线
http://www.cocoachina.com/ios/20150303/11218.html
- X264-视频帧的存取
X264的编码器结构体x264_t中的子结构体字段frames包含了4个临时视频帧序列空间:current.next.unused和reference,分别保存当前编码帧.将编码帧序列.未处理原始视频 ...
- Phoenix 无法启动报错: java.net.BindException: Address already in use
一.问题描述 i. 登录Ambari发现有一个节点的 Phoenix 无法启动 ii. 在Ambari上点击“Start”,监控 Phoenix 日志文件 iii. Phoenix 日志如下: [ro ...
- Gitlab批量迁移项目
最近接到一个需求,要把一个Gitlab上边的项目全部导入到另外一个Gitlab,借鉴了网上的一个方法,成功实现. 参考链接:https://segmentfault.com/a/11900000159 ...