骆驼解组到Pojo的jsonpath

时间:2020-07-09 14:50:02

标签: java jackson apache-camel jsonpath

我正在尝试将消息的一部分解析为pojo,以便可以在Java中对其进行处理。 该消息包含json,但是我只需要其中的一部分,因此我尝试使用jsonpath来选择所需的json部分,但后来我无法解组,我还找到了一种直接从JSON创建pojo的方法。 jsonpath,但返回null。

from(...)
.transform()
.jsonpath("@.data", false, POJO.class)
.process(...)
.end()

这使我的POJO为空。

from(...)
.transform.jsonpath("@.data")
.unmarshal().json(JsonLibrary.Jackson, POJO.class)
.process(...)
.end()

这引发异常:

Error processing exchange. Exchange[ID-5CD9519Z86-1594305984947-0-330]. Caused by: [org.apache.camel.InvalidPayloadException - No body available of type: java.io.InputStream but has value: {...}]
    at org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:118)
    at org.apache.camel.support.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:58)
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:395)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:148)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:60)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:147)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:286)
    at org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)
    at org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:40)
    at org.apache.camel.component.rabbitmq.RabbitConsumer.doHandleDelivery(RabbitConsumer.java:107)
    at org.apache.camel.component.rabbitmq.RabbitConsumer.handleDelivery(RabbitConsumer.java:82)
    at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149)
    at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:104)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: java.util.LinkedHashMap to the required type: java.io.InputStream with value {...}
    at org.apache.camel.impl.converter.CoreTypeConverterRegistry.mandatoryConvertTo(CoreTypeConverterRegistry.java:216)
    at org.apache.camel.support.MessageSupport.getMandatoryBody(MessageSupport.java:116)
    ... 15 common frames omitted

处理此问题的正确方法是什么。

预先感谢

Bart Laeremans

1 个答案:

答案 0 :(得分:0)

与产生String的xpath相反,jsonpath库产生LinkedHashMap。因此,您尝试解组LinkedHashMap而不是字符串。

例如原始json:{"books":{"book": {"name": "Some Book"}}} 预期的json:{"name": "Some Book"} jsonpath表达式:"$.books.book"

有人希望得到{"name": "Some Book"},因此解组器将产生实例 BookDto。但事实并非如此。

您可以将结果类型传递到jsonpath中,这样它将产生BookDto而不是LinkedHashMap。因此,不再需要解组。 (就像您已经做过的一样)

最后一个对我没用。

String body = exchange.getMessage().getBody(String.class);
BookDto book = JsonPath.parse(body).read("$.books.book", BookDto.class);

book为null:(

但这是JsonPath库问题,而不是骆驼