码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCloud Feign 参数问题

时间:2019-12-10 00:45:18      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:beans   actor   public   erro   ring   ppi   any   framework   app   

今天遇到使用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 参数问题

标签:beans   actor   public   erro   ring   ppi   any   framework   app   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!