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 ...
随机推荐
- Prometheus K8S中部署Alertmanager
Prometheus K8S中部署Alertmanager 设置告警和通知的主要步骤如下:一.部署Alertmanager二.配置Prometheus与Alertmanager通信三.配置告警 1. ...
- AI与数学笔记之深入浅出的讲解傅里叶变换(真正的通俗易懂)
原文出处: 韩昊 # 作 者:韩 昊 # 知 乎:Heinrich # 微 博:@花生油工人 # 知乎专栏:与时间无关的故事 # 谨以此文献给大连海事大学的吴楠老师,柳晓鸣老师,王新年老师以及张 ...
- 【机器学习笔记】ID3构建决策树
好多算法之类的,看理论描述,让人似懂非懂,代码走一走,现象就了然了. 引: from sklearn import tree names = ['size', 'scale', 'fruit', 'b ...
- C# - VS2019调用AForge库实现调用摄像头拍照功能
前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...
- Python - 模块 - 第十六天
Python 模块 在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办法 ...
- python算法题 python123网站单元四题目
目录 一:二分法求平方根 二:Collatz猜想 三:算24(只考虑满足,不考虑把所有情况找出来) 下面向大家介绍几个python算法题. 一:二分法求平方根 1.题目要求为 2.输入输出格式为 ...
- 解决关于 npm build --prod ,出现 ERROR in budgets, maximum exceeded for initial. Budget 5 MB was exceeded by 750 kB的问题
问题: 执行命令 :npm build --pord,出现以下错误: WARNING :. Ignoring. WARNING MB was exceeded by 3.73 MB. ERROR MB ...
- Docker Desktop for Windows 安装步骤
Docker Desktop for Windows 安装要求 Docker Desktop for Windows需要运行Microsoft Hyper-V.如果需要,Docker Desktop ...
- WinRAR命令行版本 rar.exe使用详解(适用Linux)
RAR 命令行语法: RAR.exe <命令> [ -<开关> ] <压缩文件> [ <@列表文件...> ] [ <文件...> ] [ ...
- 不了解MES系统中的看板管理?看完本文就懂了
如果想要在生产车间中,让生产过程管理都处在“看得见”的状态,那么看板可视化管理的导入是你的不二选择. MES看板包括四个部分:生产任务看板.各生产单位生产情况看板.质量看板和物料看板,其中生产任务看板 ...