问题描述
有关代码,请参见我的微小4类 github项目
For code, see my tiny 4 class github project
我正在使用Spring FeignClients连接到Rest服务. Feign客户端的基本(非异步)形式如下所示:
I am using Spring FeignClients to connect to a rest service. This is what the Feign client looks like in its basic (non-async) form:
@FeignClient(value="localhost:8080/products", decode404 = true)
public interface ProductClient {
@RequestMapping(value="/{id}")
Product getById(@PathVariable("id") String id);
}
现在,我想使用Observable异步进行此操作. Spring文档中严重缺乏有关此方面的信息,只有一小段,告诉您使用HystrixCommand.仅此而已,没有解释,没有样本代码.
Now I wanted to do that asynchronously, using an Observable. Information on this is severely lacking in the Spring docs, there is only a small paragraph that tells you to use a HystrixCommand. That's all, no explanation, no sampe code.
在另一篇博客文章中,有人告诉我改用HystrixObservable.所以我尝试了:
In another blog post, I was told to use a HystrixObservable instead. And so I tried that:
@FeignClient(value="localhost:8080/products", decode404 = true)
public interface ProductClient {
@RequestMapping(value="/{id}")
HystrixObservable<Product> getById(@PathVariable("id") String id);
}
无论哪种方式,使用HystrixCommand或HystrixObservable,都会抛出错误: com.fasterxml.jackson.databind.JsonMappingException:无法构造com.netflix.hystrix.HystrixObservable的实例
Either way, with a HystrixCommand or HystrixObservable, it throws me the error:com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.netflix.hystrix.HystrixObservable
我理解为什么会出现该错误,因为Spring Boot会自动将一个Decoder附加到FeignClient上,以使用Jackson来反序列化响应.从响应值派生反序列化为响应的类型.
I understand why it is giving that error, since Spring Boot automatically attaches a Decoder to the FeignClient to deserialize the response using Jackson. And the type to deserialize the response into is derived from the return value.
我可以尝试配置自定义解码器或手动构建Feign客户端,但这有点违背了Spring Boot的全部目的:它可以自动运行(尽管在这里和那里有一些配置).
I could try to configure a custome Decoder or manually build the Feign clients, but that kind-of defeats the whole purpose of Spring Boot: that it works automagically (albeit with a bit of configuration here and there).
所以我的问题是:这应该如何工作?
And so my question is: how is this supposed to work?
推荐答案
如果有人在升级到spring-cloud:1.3.+后开始出现此错误,请确保已启用hystrix-feign.
If anybody starts getting this error after upgrading to spring-cloud:1.3.+ make sure you have hystrix-feign enabled.
feign.hystrix.enabled=true
已添加此功能,因此假装默认情况下不会将调用包装在hystrix命令中.
This was added so feign does not wrap calls in hystrix commands by default.
https://github.com/spring-cloud/spring -cloud-netflix/issues/1277
这篇关于春云netflix和HystrixObservable-> JsonMappingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!