问题一:
- @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了。
问题三:多参数问题
- @RequestMapping(value="/user/name", method=RequestMethod.GET)
- User findByUsername(final String userName, final String address);
启动服务的时候,会报如下异常:
- 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里面可以不加该注解修饰。
上面问题的解决方案如下:
- @RequestMapping(value="/user/name", method=RequestMethod.GET)
- User findByUsername(@RequestParam("userName") final String userName, @RequestParam("address") final String address);
问题四:Request method 'POST' not supported
错误代码示例:
- @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本身是没有上传文件的能力的,要想实现这一点,需要自己去编写Encoder 去实现上传。现在我们幸福了很多。因为Feign官方提供了子项目feign-form ,其中实现了上传所需的 Encoder 。
注:笔者测试的版本是Edgware.RELEASE。Camden、Dalston同样适应本文所述。
加依赖
1 2 3 4 5 6 7 8 9 10 | |
编写Feign Client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
如代码所示,在这个Feign Client中,我们引用了配置类MultipartSupportConfig ,在MultipartSupportConfig 中,我们实例化了SpringFormEncoder 。这样这个Feign Client就能够上传啦。
注意点
1 2 3 4 | |
接口定义中的注解@RequestPart(value = "file") 不能写成@RequestParam(value = "file" 。
最好将Hystrix的超时时间设长一点,例如5秒,否则可能文件还没上传完,Hystrix就超时了,从而导致客户端侧的报错。
SpringCloud中使用Feign的坑
示例如下:
1 2 3 4 5 6 7 8 9 |
|
说明:
*使用RequestMapping中的consumes指定生成的请求的Content-Type
*RequestParam指定的参数会拼接在URL之后,如: ?name=xxx&age=18
*PathVariable指定的参数会放到一个LinkedHashMap<String, ?>传入到feign的Encoder中进行处理,而在Spring中实现了该接口的Encoder为SpringEncoder,而该实现又会使用Spring中的HttpMessageConverter进行请求体的写入。
坑:
*不要在接口类名上使用RequestMapping,虽然可以使用,但同时SpringMVC会把该接口的实例当作Controller开放出去,这个可以在启动的Mapping日志中查看到
*使用默认的SpringEncoder,在不指定consumes时,PathVariable中的参数会生成JSON字符串发送,且默认情况下不支持Form表单的生成方式,原因为:FormHttpMessageConverter只能处理MultiValueMap,而使用PathVariable参数被放在了HashMap中。默认更不支持文件上传。其实已经有支持处理各种情况的HttpMessageConverter存在。
填坑:
*支持Form表单提交:只需要编写一个支持Map的FormHttpMessageConverter即可,内部可调用FormHttpMessageConverter的方法简化操作。
*支持文件上传:只需要把要上传的文件封装成一个Resource(该Resource一定要实现filename接口,这个是把请求参数解析成文件的标识),使用默认的ResourceHttpMessageConverter处理即可。
*支持处理MultipartFile参数:编写一个支持MultipartFile的MultipartFileHttpMessageConverter即可,内部可调用ResourceHttpMessageConverter实现,同时注意需要将其添加至FormHttpMessageConverter的Parts中,并重写FormHttpMessageConverter的getFilename方法支持从MultipartFile中获取filename
*所有的HttpMessageConverter直接以@Bean的方式生成即可,spring会自动识别添加
完美支持表单和文件上传:
方案一:
使用附件中的MapFormHttpMessageConverter.java和MultipartFileHttpMessageConverter.java
在Spring中进行如下配置即可
1 2 3 4 5 6 7 8 9 10 11 |
|
方案二:
使用FeignSpringFormEncoder.java
在Spring中配置如下:
1 2 3 4 | |