Apache的骆驼和杰克逊 [英] apache camel and jackson
问题描述
我想了Apache的骆驼,和我已经成立了,它通过http4组件调用HTTP服务基本路线,通过转换解组()的结果。JSON(JsonLibrary.Jackson),然后打印出的一部分在一个Bean组件的响应。
我遇到的问题是,它在运行时鼓起时,它得到的JSON unmarhsaller:
没有可用的类型转换器从类型转换:要的java.util.HashMap所需的类型:com.xxx.MyType
块引用>响应的格式如下:
<$ C C> {数据:[{X:Y},{X:Z}]}
和我的对象模型是这样的:
@ lombok.Data
级别响应{
私人列表&LT;&ELEM GT;数据;
}@ lombok.Data
类ELEM {
私人字符串x;
}所以它会出现在解组认为响应是一个哈希映射,而我希望它解组为对象结构。有没有办法得到它做我想做什么?
解决方案找到了答案,万一有人张贴其他运行到这一点。路线建设者应该设置这样的:
<$p$p><$c$c>from(\"direct:start\").to(\"http4://...\").unmarshal().json(JsonLibrary.Jackson,com.xxx.Response)
。要(豆:com.xxx.MyResponseEchoer)
即。通过类类型的JSON方法。
I'm trying out apache-camel, and I've set up a basic route that calls an http service via http4 component, transforms the result via unmarshal().json(JsonLibrary.Jackson), and then prints out part of the response in a bean component.
The problem I'm having is that it blows up at runtime when it gets to the json unmarhsaller:
No type converter available to convert from type: java.util.HashMap to the required type: com.xxx.MyType
The response is of this format:
{"data":[{"x":"y"},{"x":"z"}]}
And my object model is like:
@lombok.Data class Response { private List<Elem> data; } @lombok.Data class Elem { private String x; }
So it would appear that the unmarshaller thinks the response is a hash map, whereas I want it to unmarshal into an object structure. Is there a way to get it to do what I want?
解决方案Found the answer, posting in case anyone else runs into this. The route builder should be setup like:
from("direct:start").to("http4://...").unmarshal().json(JsonLibrary.Jackson,com.xxx.Response) .to("bean:com.xxx.MyResponseEchoer")
I.e. pass the class type to the json method.
这篇关于Apache的骆驼和杰克逊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!