SpringCloud Feign参数问题及解决方法
作者:慕尘 发布时间:2022-08-08 08:00:35
这篇文章主要介绍了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请求
来源:https://www.cnblogs.com/baby123/p/12014117.html


猜你喜欢
- 一、技术介绍1.chatgpt-java是一个OpenAI的Java版SDK,支持开箱即用。目前以支持官网全部Api。支持最新版本GPT-3
- 1,LuBan压缩问题 https://github.com/Curzibn/Luban之前选择压缩图片
- 本文主要介绍了C# 泛型List排序的实现,分享给大家,具体如下:代码using System;using System.Collectio
- 布局文件中的TextView属性<TextViewandroid:id="@+id/businesscardsingle_c
- 前言工作中是否有这样的场景,多个线程任务,如果所有线程完成到某个阶段,你希望知道所有线程均完成该阶段。当然你使用线程计数可以实现,只是不够优
- 本文实例讲述了C#实现系统托盘通知的方法。分享给大家供大家参考。具体实现方法如下:namespace WindowsApplication1
- 一.介绍观察者模式(Observer Pattern)属于行为型模式。定义了对象之间的一对多依赖,让多个观察者同时监听某一个主题对象,类似于
- 之前的工作中,需要实现一个功能就是GridView中的item可以自由拖动, 思考了一下,其实实现起来不是很困难,主要工作就是交换节点,以及
- 前言在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页。前端分页一次
- public class PersonAdapter extends BaseAdapter { private List per
- springboot logback动态获取application的配置项在多环境的情况下,logback的日志路径需要进行针对性配置,也就
- 本文实例讲述了C#实现图片切割的方法。分享给大家供大家参考,具体如下:图片切割就是把一幅大图片按用户要求切割成多幅小图片。dotnet环境下
- zuul添加或修改请求参数一、为什么要用到这个在基于 springcloud 构建的微服务系统中,通常使用网关zuul来进行一些用户验证等过
- HttpResponse 讲解HttpServletResponse概述:在创建Servlet时会覆盖service()方法,或doGet(
- Configuration configuration = ConfigurationManager.OpenExeConfiguratio
- XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便。对于XML本身的语法知识
- 引言Java反射机制是一个非常强大的功能,在很多大型项目比如Spring, Mybatis都可以看见反射的身影。通过反射机制我们可以在运行期
- 使用Zenject和UniRx的入门级技术实现了伪登录注册功能。运行效果登录面板using System;using UniRx;using
- 方式1. 使用HashtableMap<String,Object> hashtable=new Hashtable
- 1)1、1、2、3、5、8.......用递归算法求第30位数的值?首先我们能够发现从第3位数起后一位数等于前两位数值之和,即:x=(x-1