SpringCloud Feign 参数问题

今天遇到使用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修饰的就要放在提交对象中

猜你喜欢

转载自www.cnblogs.com/baby123/p/12014117.html