Java ApacheCamel-将Rest DSL中的数据编组为Json
我正在尝试使用Camel中的Rest DSL处理csv文件 当我将CSV和Marshall拆分为JSON时,会出现一些奇怪的行为。这是我的密码:Java ApacheCamel-将Rest DSL中的数据编组为Json,java,apache-camel,Java,Apache Camel,我正在尝试使用Camel中的Rest DSL处理csv文件 当我将CSV和Marshall拆分为JSON时,会出现一些奇怪的行为。这是我的密码: @Component public class ProcessHandler extends RouteBuilder { @Override protected void defineRoute() throws Exception { DataFormat csv = new BindyCsvDataFormat
@Component
public class
ProcessHandler extends RouteBuilder {
@Override
protected void defineRoute() throws Exception {
DataFormat csv = new BindyCsvDataFormat(CsvModel.class);
rest("/")
.post().produces("application/json")
.route()
.unmarshal(csv)
.split(body()).parallelProcessing().streaming()
.marshal().json(JsonLibrary.Gson)
.filter().jsonpath("$[?(@.counter==3)]")
.log("${body}")
但是,我得到了错误消息:在从type:java.lang.String转换为所需的type:byte[]并带有值[CsvModel(…
但是,如果我按如下方式编辑路线:
@Component
public class
ProcessHandler extends RouteBuilder {
@Override
protected void defineRoute() throws Exception {
DataFormat csv = new BindyCsvDataFormat(CsvModel.class);
rest("/")
.post().produces("application/json")
.route()
.unmarshal(csv)
.marshal().json(JsonLibrary.Gson)
.split(body()).parallelProcessing().streaming()
//.filter().jsonpath("$[?(@.counter==3)]")
.log("${body}")
它工作正常
但是,我显然无法正确处理消息,因为它被编组为字节表示。如果不使用Rest DSL,路由也可以正常工作,因此我假设问题在于http响应。如果我尝试以下操作:
@Component
public class
ProcessHandler extends RouteBuilder {
@Override
protected void defineRoute() throws Exception {
DataFormat csv = new BindyCsvDataFormat(CsvModel.class);
rest("/")
.post().produces("application/json")
.route()
.unmarshal(csv)
.marshal().json(JsonLibrary.Gson)
.split(body()).parallelProcessing().streaming()
.unmarshal().json(JsonLibrary.Gson)
.filter().jsonpath("$[?(@.counter==3)]")
.marshal().json(JsonLibrary.Gson)
.log("${body}")
我也遇到了同样的错误。是否可以将格式标准化,进行一些处理步骤,然后返回Json?我是否在某个地方出错了?我希望理解为什么会发生这种情况?如果您在其中使用过滤而不是使用JsonPath实现聚合策略,可能会更容易理解 实际上,默认情况下使用的
split()
methid不会给出您期望的结果
以下是一个例子:
@Component
public class ProcessHandler extends RouteBuilder {
@Override
protected void defineRoute() throws Exception {
DataFormat csv = new BindyCsvDataFormat(CsvModel.class);
rest("/")
.post().produces("application/json")
.route()
.unmarshal(csv)
.split().method(ItemsSplittingStrategy.class, "splitItems")
.parallelProcessing()
.marshal().json(JsonLibrary.Gson)
.end()
.to("file:/file.json");
}
}
在你的itemsplitingstrategy
类中,你可以找到一个简单明确的例子
此外,我还邀请您检查可用于拆分器和聚合器及其组合的所有功能。谢谢,这很有用,而且效果更好。我有点搞不清楚为什么
筛选器
EIP在拆分器
EIP之后不起作用。例如,当我添加.filter().jsonpath($[?(@.counter==3)])时
它现在在编码方面没有问题,但似乎没有实际过滤消息..?这与exchange中的输出类型有关。在您的代码中,在使用jsonPath应用过滤器后,您正在解组JSON。我会在解组JSON之前进行过滤。谢谢。鉴于数据非常大,我宁愿split
然后stream
消息,以避免产生巨大的内存开销。您知道任何解决方法吗?谢谢您的帮助!使用并行处理是一件好事。此外,您还可以在策略中包括一个批处理系统来分块数据。