首页 > 编程语言 > 详细

SpringCloud Feign 参数问题

时间:2019-12-10 01:35:43      阅读:426      评论:0      收藏:0      [点我收藏+]

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

1.RequestParam.value() was empty on parameter 0

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

解决方法:

  加上注解的描述,修改为

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

如果是@RequestBody不需要注解的描述

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

2.Feign多参数的问题

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

遇到报错Method has too many Body parameters

具体描述如下

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘feignController‘: Unsatisfied dependency expressed through field ‘remoteHelloService‘; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com.xyz.comsumer.feign.RemoteHelloService‘: FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract java.lang.String com.xyz.comsumer.feign.RemoteHelloService.test(java.lang.String,java.lang.Integer)

正确的写法

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

总结:

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

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

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

SpringCloud Feign 参数问题

原文:https://www.cnblogs.com/baby123/p/12014117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!