我从一个rest-interface获得一个json数组作为输入,基于ond apache-camel-spark。一开始,我想分割json数组,通过apache camels路由处理每个元素。我是怎么做到的?
我的test-input-json:
[
{
"document": {
"name": "blub1",
"type": "pdf"
}
},
{
"document": {
"name": "blub2",
"type": "pdf"
}
}
];
对于这个问题,我在stackoverflow上发现了一些问题,这些问题间接地描述了这个问题:
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");
当我这样做时,我总是得到以下例外:
错误DefaultErrorHandler - (MessageId: [...]关于ExchangeId:[...])。分娩后用尽:1 catch:org.apache.camel.ExpressionEvaluationException: 显示java.lang.NullPointerException
我没有找到原因,我的表情是什么?我改变了它,但我总是得到NullPointerException。
我下载了JavaScript jsonPath library并测试了$..document.
表达式,它为我提供了包含两个对象的数组。我也在camel中试过它,它说,在test-input-json中找到了表达式(返回true):
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$1.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$2.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$3.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
答案 0 :(得分:2)
尝试创建类似于:
的jsonpathExpressionBuilder.languageExpression("jsonpath","$..document.")
并将其放入您的拆分并尝试。在进行各种条件评估时,我通过这种方式获得了更多的成功。