spring cloud-Feign使用中遇到的问题总结
2018-01-17 11:42
417 查看
转载:http://blog.csdn.net/liuchuanhong1/article/details/54728681
问题一:
在前面的示例中,我们讲过
[java] view
plain copy
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
@GetMapping("/user/{id}")
这两个注解的效果是等价的,但是在Feign使用中,只能用上面的那种方式,不能直接用@GetMapping,下面我们将前面的那个示例中,改成@GetMapping注解看下效果,我们发现,修改注解后重新启动服务的时候,抛了如下异常:
[java] view
plain copy
Caused by: java.lang.IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST)
异常的意思是,我们没有指定HTTP的方法
问题二:
在前面的示例中,我们暴漏Restful服务的方式如下:
[java] view
plain copy
@GetMapping("/template/{id}")
public User findById(@PathVariable Long id) {
return client.findById(id);
}
这里,findById方法的参数中,我们直接使用了
[java] view
plain copy
@PathVariable Long id
下面我们将Feign的方式也改成这种
[java] view
plain copy
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
User findById(@PathVariable Long id);
然后启动服务,我们发现,又抛异常了,异常信息如下:
[java] view
plain copy
Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
大概的意思是PathVariable注解的第一个参数不能为空,我们改成如下的方式:
[java] view
plain copy
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
User findById(@PathVariable("id") Long id);
再启动,发现一切都ok了。
问题三:多参数问题
[java] view
plain copy
@RequestMapping(value="/user/name", method=RequestMethod.GET)
User findByUsername(final String userName, final String address);
启动服务的时候,会报如下异常:
[java] view
plain copy
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
@RequestMapping(value="/user/name", method=RequestMethod.GET)
User findByUsername(@RequestParam("userName") final String userName, @RequestParam("address") final String address);
问题四:Request method 'POST' not supported
错误代码示例:
[java] view
plain copy
@RequestMapping(value="/user/name", method=RequestMethod.GET)
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请求发送出去的。
问题一:
在前面的示例中,我们讲过
[java] view
plain copy
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
@GetMapping("/user/{id}")
这两个注解的效果是等价的,但是在Feign使用中,只能用上面的那种方式,不能直接用@GetMapping,下面我们将前面的那个示例中,改成@GetMapping注解看下效果,我们发现,修改注解后重新启动服务的时候,抛了如下异常:
[java] view
plain copy
Caused by: java.lang.IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST)
异常的意思是,我们没有指定HTTP的方法
问题二:
在前面的示例中,我们暴漏Restful服务的方式如下:
[java] view
plain copy
@GetMapping("/template/{id}")
public User findById(@PathVariable Long id) {
return client.findById(id);
}
这里,findById方法的参数中,我们直接使用了
[java] view
plain copy
@PathVariable Long id
下面我们将Feign的方式也改成这种
[java] view
plain copy
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
User findById(@PathVariable Long id);
然后启动服务,我们发现,又抛异常了,异常信息如下:
[java] view
plain copy
Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.
大概的意思是PathVariable注解的第一个参数不能为空,我们改成如下的方式:
[java] view
plain copy
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
User findById(@PathVariable("id") Long id);
再启动,发现一切都ok了。
问题三:多参数问题
[java] view
plain copy
@RequestMapping(value="/user/name", method=RequestMethod.GET)
User findByUsername(final String userName, final String address);
启动服务的时候,会报如下异常:
[java] view
plain copy
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
@RequestMapping(value="/user/name", method=RequestMethod.GET)
User findByUsername(@RequestParam("userName") final String userName, @RequestParam("address") final String address);
问题四:Request method 'POST' not supported
错误代码示例:
[java] view
plain copy
@RequestMapping(value="/user/name", method=RequestMethod.GET)
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请求发送出去的。
相关文章推荐
- spring cloud-Feign使用中遇到的问题总结
- 详解spring cloud Feign使用中遇到的问题总结
- spring cloud 使用feign 遇到问题
- spring cloud-Feign使用中遇到的问题总结
- 使用Entity Framework时遇到的各种问题总结
- 转 [开发总结]WebView使用中遇到的一些问题&解决
- ubuntu 使用中遇到的问题总结
- .net在使用Lucene中遇到的一些问题总结
- 在使用Struts的过程中也遇到了一些问题,现在总结一下
- 使用VS2010时遇到的一些小问题的解决方案(从各处搜罗总结的,不定期更新,向原作者致敬)
- nginx使用遇到的问题总结
- 百度地图ios api使用中遇到的问题总结
- MongoDB安装成为Windows服务及日常使用遇到问题总结
- oracle 11g使用deferred_segment_creation 延迟段创建特性时遇到的问题总结
- 使用css进行网页排版中遇到的一些问题总结
- 使用Pluto+Tomcat 开发遇到的问题和总结 (2)
- 2011.11.12使用MDK遇到的问题总结
- weblogic10使用时遇到的问题总结
- 使用Pluto+Tomcat 开发遇到的问题和总结 (1)
- 百度地图api使用中遇到的问题和解决方案总结