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

来源:互联网 发布:制作mv视频软件 编辑:程序博客网 时间:2024/05/29 23:23

问题一:

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

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

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

问题二:

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

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

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

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

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)User findById(@PathVariable("id") Long id);
再启动,发现一切都ok了。

1 0
原创粉丝点击