如何在 apache camel 中拆分 json 数组

how to split a json array in apache camel

我从基于 apache-camel-spark 的 rest-interface 得到一个 json 数组作为输入。一开始,我想拆分 json-array 以通过 apache camels 路由单独处理每个元素。我该怎么做?

我的测试输入-json:

[
  {
      "document": {
        "name": "blub1",
        "type": "pdf"
      }
  },
  {
      "document": {
        "name": "blub2",
        "type": "pdf"
      }
  }
];

对于这个问题,我在Whosebug上找到了一些问题,间接描述了这一点:
link 1, link 2, link 3.

根据示例,我尝试了以下骆驼路线:

restConfiguration().component("spark-rest").port(8080)
  .bindingMode(RestBindingMode.json)
  .dataFormatProperty("prettyPrint", "true");

rest("/v1/users").consumes("application/json").produces("application/json")
  .post("/insert")
  .to("direct:split");

from("direct:split")
  .marshal().json(JsonLibrary.Jackson)
  .split(new JsonPathExpression("$..document.")).process(new Processor() {
      public void process(Exchange exchange) throws Exception {
         String s = exchange.getIn().getBody(String.class);
         System.out.println(s);
      }
   }).to("file:outbox");

当我这样做时,我总是得到以下异常:

ERROR DefaultErrorHandler - Failed delivery for (MessageId: [...] on ExchangeId: [...]). Exhausted after delivery attempt: 1 caught: org.apache.camel.ExpressionEvaluationException: java.lang.NullPointerException

我没找到原因,我的表情怎么了?我更改了它,但我总是得到 NullPointerException。

我下载了 JavaScript jsonPath library 并测试了 $..document. 表达式,它为我提供了一个包含两个对象的数组。我也在骆驼中尝试过,它说,在测试输入中找到了表达式(returns true)-json:

from("direct:testExpression")
        .marshal().json(JsonLibrary.Jackson)
        .choice()
            .when().jsonpath("$..document.", true)
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                String payload = exchange.getIn().getBody(String.class);
                exchange.getIn().setBody("true");
                System.out.println(payload);
                }
            })
            .to("file:outbox")
        .otherwise()
            .process(new Processor() {
                public void process(Exchange exchange) throws Exception {
                String payload = exchange.getIn().getBody(String.class);
                exchange.getIn().setBody("false");
                System.out.println(payload);
                }
            })
            .to("file:outbox");

我错了什么,为什么我得到 NullPointerException 以及如何拆分 json?

这里是完整的 Stacktrace:

org.apache.camel.ExpressionEvaluationException: java.lang.NullPointerException
    at org.apache.camel.jsonpath.JsonPathExpression.evaluate(JsonPathExpression.java:95) ~[camel-jsonpath-2.18.0.jar:2.18.0]
    at org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:36) ~[camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.Splitter.createProcessorExchangePairs(Splitter.java:113) ~[camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.MulticastProcessor.process(MulticastProcessor.java:231) ~[camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.Splitter.process(Splitter.java:108) ~[camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:120) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:83) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97) [camel-core-2.18.0.jar:2.18.0]
    at org.apache.camel.component.sparkrest.CamelSparkRoute.handle(CamelSparkRoute.java:46) [camel-spark-rest-2.18.0.jar:2.18.0]
    at spark.RouteImpl.handle(RouteImpl.java:58) [spark-core-2.3.jar:?]
    at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162) [spark-core-2.3.jar:?]
    at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61) [spark-core-2.3.jar:?]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.server.Server.handle(Server.java:499) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257) [jetty-server-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.io.AbstractConnection.run(AbstractConnection.java:544) [jetty-io-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) [jetty-util-9.2.19.v20160908.jar:9.2.19.v20160908]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:555) [jetty-util-9.2.19.v20160908.jar:9.2.19.v20160908]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101]
Caused by: java.lang.NullPointerException
    at org.apache.camel.jsonpath.JsonPathExpression.evaluateJsonPath(JsonPathExpression.java:118) ~[camel-jsonpath-2.18.0.jar:2.18.0]
    at org.apache.camel.jsonpath.JsonPathExpression.evaluate(JsonPathExpression.java:88) ~[camel-jsonpath-2.18.0.jar:2.18.0]
    ... 31 more

尝试创建您的 jsonpath 表达类似于:

ExpressionBuilder.languageExpression("jsonpath","$..document.") 

并将其放入您的拆分中并尝试。在进行各种条件评估时,我以这种方式使用 jsonpath 取得了更大的成功。