本文介绍了Apache Camel:使用Camel方法将JSON转换为POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST服务器,该服务器在响应正文中发送JSON.我最近开始阅读有关Apache Camel的文章.我使用以下命令将请求发送到我的REST服务.

I have a REST server which sends JSON in response body. I have recently started reading about Apache Camel. I use following to send requests to my REST service.

from("direct:start").setHeader("token", simple("234da"))
                            .to("http://localhost:8088/foo/bar/?foo1=bar1");

现在响应将是一个JSON,我有什么办法可以使用 to()之前的某种方法(例如类似的东西)将这个JSON直接放入POJO中?

Now the response will be a JSON, is there any way I get this JSON directly into a POJO using some method ahead of to() (something like this)?

to("http://localhost:8088/foo/bar/?foo1=bar1").toPOJO();

我希望使用非Spring解决方案.

I would prefer a non Spring solution.

谢谢

推荐答案

我身边的细节很少-虽然迟了

Little details from my side - although late

创建jsonFormatter,然后与所需的类解组
JsonDataFormat jsonDataFormat =新的JsonDataFormat(JsonLibrary.Jackson);
可以用于编组

Create jsonFormatter and then unmarshal with class you need
JsonDataFormat jsonDataFormat = new JsonDataFormat(JsonLibrary.Jackson);
this can be used in marshalling

from("direct:consume-rest")
.log(正在调用bean方法...")
.to("http://localhost:8080/greeting?name = baba")
//.process(svProcessor)//如果需要,可以进行任何其他处理
.unmarshal().json(JsonLibrary.Jackson,Greeting.class)
.bean(GreetingHelper.class,"print")
.log(转换为bean ...")
.end()
;

from("direct:consume-rest")
.log("calling bean method...")
.to("http://localhost:8080/greeting?name=baba")
//.process(svProcessor) // any extra process if you want
.unmarshal().json(JsonLibrary.Jackson, Greeting.class)
.bean(GreetingHelper.class, "print")
.log("converted to bean ...")
.end()
;

Helper类方法
公共无效打印(@Body Greeting){

Helper class method
public void print (@Body Greeting greeting) {

这篇关于Apache Camel:使用Camel方法将JSON转换为POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 17:10