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

问题一:

  1. @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)  
  2. @GetMapping("/user/{id}")  

这两个注解的效果是等价的,但是在Feign使用中,只能用上面的那种方式,不能直接用@GetMapping,下面我们将前面的那个示例中,改成@GetMapping注解看下效果,我们发现,修改注解后重新启动服务的时候,抛了如下异常:

  1. Caused by: java.lang.IllegalStateException: Method findById not annotated with HTTP method type (ex. GET, POST)  

异常的意思是,我们没有指定HTTP的方法

问题二:

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

  1. @GetMapping("/template/{id}")  
  2.     public User findById(@PathVariable Long id) {  
  3.         return client.findById(id);  
  4.     }  

这里,findById方法的参数中,我们直接使用了

  1. @PathVariable Long id  

下面我们将Feign的方式也改成这种

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

然后启动服务,我们发现,又抛异常了,异常信息如下:

  1. Caused by: java.lang.IllegalStateException: PathVariable annotation was empty on param 0.  

大概的意思是PathVariable注解的第一个参数不能为空,我们改成如下的方式:

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

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

问题三:多参数问题

  1. @RequestMapping(value="/user/name", method=RequestMethod.GET)  
  2.     User findByUsername(final String userName,  final String address);  

启动服务的时候,会报如下异常:

  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里面可以不加该注解修饰。

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

  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

错误代码示例:

  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请求发送出去的。

 

问题五:使用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

<dependency>

 <groupId>io.github.openfeign.form</groupId>

 <artifactId>feign-form</artifactId>

 <version>3.0.3</version>

</dependency>

<dependency>

 <groupId>io.github.openfeign.form</groupId>

 <artifactId>feign-form-spring</artifactId>

 <version>3.0.3</version>

</dependency>


编写Feign Client

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@FeignClient(name = "ms-content-sample", configuration = UploadFeignClient.MultipartSupportConfig.class)

public interface UploadFeignClient {

 @RequestMapping(value = "/upload", method = RequestMethod.POST,

   produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},

   consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

 @ResponseBody

 String handleFileUpload(@RequestPart(value = "file") MultipartFile file);

 class MultipartSupportConfig {

  @Bean

  public Encoder feignFormEncoder() {

   return new SpringFormEncoder();

  }

 }

}

如代码所示,在这个Feign Client中,我们引用了配置类MultipartSupportConfig ,在MultipartSupportConfig 中,我们实例化了SpringFormEncoder 。这样这个Feign Client就能够上传啦。

注意点

1

2

3

4

@RequestMapping(value = "/upload", method = RequestMethod.POST,

   produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},

   consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

中的produeces 、consumes 不能少;

接口定义中的注解@RequestPart(value = "file") 不能写成@RequestParam(value = "file" 。

最好将Hystrix的超时时间设长一点,例如5秒,否则可能文件还没上传完,Hystrix就超时了,从而导致客户端侧的报错。

SpringCloud中使用Feign的坑

示例如下:

1

2

3

4

5

6

7

8

9

@FeignClient("service-resource")

//@RequestMapping("/api/test")

public interface TestResourceItg {

 

 @RequestMapping(value = "/api/test/raw", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")

 public String raw1(@PathVariable("subject") String subject, // 标题

     @RequestParam("content") String content); // 内容

 

}

说明:

*使用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

@Bean

public MapFormHttpMessageConverter mapFormHttpMessageConverter(MultipartFileHttpMessageConverter multipartFileHttpMessageConverter) {

 MapFormHttpMessageConverter mapFormHttpMessageConverter = new MapFormHttpMessageConverter();

 mapFormHttpMessageConverter.addPartConverter(multipartFileHttpMessageConverter);

 return mapFormHttpMessageConverter;

}

 

@Bean

public MultipartFileHttpMessageConverter multipartFileHttpMessageConverter() {

 return new MultipartFileHttpMessageConverter();

}

方案二:

使用FeignSpringFormEncoder.java

在Spring中配置如下:

1

2

3

4

@Bean

public Encoder feignEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {

 return new FeignSpringFormEncoder(messageConverters);

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值