spring cloud-Feign使用中遇到的问题总结

问题一:

在前面的示例中,我们讲过

[java]  view plain  copy
  1. @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)  
  2. @GetMapping("/user/{id}")  
这两个注解的效果是等价的,但是在Feign使用中,只能用上面的那种方式,不能直接用@GetMapping,下面我们将前面的那个示例中,改成@GetMapping注解看下效果,我们发现,修改注解后重新启动服务的时候,抛了如下异常:

[java]  view plain  copy
  1. Caused by: java.lang.IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST)  
异常的意思是,我们没有指定HTTP的方法

问题二:

在前面的示例中,我们暴漏Restful服务的方式如下:

[java]  view plain  copy
  1. @GetMapping("/template/{id}")  
  2.     public User findById(@PathVariable Long id) {  
  3.         return client.findById(id);  
  4.     }  
这里,findById方法的参数中,我们直接使用了
[java]  view plain  copy
  1. @PathVariable Long id  
下面我们将Feign的方式也改成这种

[java]  view plain  copy
  1. @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)  
  2.     User findById(@PathVariable Long id);  
然后启动服务,我们发现,又抛异常了,异常信息如下:

[java]  view plain  copy
  1. Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.  
大概的意思是PathVariable注解的第一个参数不能为空,我们改成如下的方式:

[java]  view plain  copy
  1. @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)  
  2.     User findById(@PathVariable("id") Long id);  

再启动,发现一切都ok了。

问题三:多参数问题

[java]  view plain  copy
  1. @RequestMapping(value="/user/name", method=RequestMethod.GET)  
  2.     User findByUsername(final String userName,  final String address);  
启动服务的时候,会报如下异常:

[java]  view plain  copy
  1. Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.chhliu.springboot.restful.vo.User com.chhliu.springboot.restful.feignclient.UserFeignClient.findByUsername(java.lang.String,java.lang.String)  

异常原因:当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上@RequestParam注解修饰,Controller里面可以不加该注解修饰。

上面问题的解决方案如下:

[java]  view plain  copy
  1. @RequestMapping(value="/user/name", method=RequestMethod.GET)  
  2.     User findByUsername(@RequestParam("userName"final String userName, @RequestParam("address"final String address);  

问题四:Request method 'POST' not supported

错误代码示例:

[java]  view plain  copy
  1. @RequestMapping(value="/user/name", method=RequestMethod.GET)  
  2.     User findByUsername(final String userName, @RequestParam("address"final String address);  

注意:上面的userName参数没有用@RequestParam注解修饰,然后发送请求,会发现被调用的服务一直报Request method 'POST' not supported,我们明明使用的是GET方法,为什么被调用服务认为是POST方法了,原因是当userName没有被@RequestParam注解修饰时,会自动被当做request body来处理。只要有body,就会被feign认为是post请求,所以整个服务是被当作带有request parameter和body的post请求发送出去的。


猜你喜欢

转载自blog.csdn.net/weixin_38809962/article/details/80354864