Apache Camel JSON 编组到 POJO Java Bean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40756027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:42:54  来源:igfitidea点击:

Apache Camel JSON Marshalling to POJO Java Bean

jsonapache-camelmarshallingunmarshalling

提问by bicster

I think I have a simple question, but can't seem to figure it out.

我想我有一个简单的问题,但似乎无法弄清楚。

I'm invoking a POJO with a class created from unmarshalling JSON as the parameter for the method. The question is, how do I marshal the return from the method back to JSON?

我正在使用一个通过解组 JSON 创建的类作为该方法的参数来调用 POJO。问题是,如何将方法的返回编组回 JSON?

My route is below;

我的路线如下;

from("direct:start")
 .choice()
  .when(header("methodname").isEqualTo("listCases"))
   .unmarshal().json(JsonLibrary.Hymanson, UserDetails.class)
   .to("bean:com.xxx.BeanA")
  .when(header("methodName").isEqualTo("listPersons"))
   .unmarshal().json(JsonLibrary.Hymanson, CaseDetails.class)
   .to("bean:com.xxx.BeanB");

...and I'm invoking the route by the below;

...我正在调用下面的路线;

ProducerTemplate template = camelContext.createProducerTemplate();
template.setDefaultEndpoint(camelContext.getEndpoint("direct:start"));
InvocationResult result = (InvocationResult)template.requestBodyAndHeader(payload, "methodName", methodName);

Payload is JSON, and the methodName is either listCases or listPersons in this example.

Payload 是 JSON,在本示例中,methodName 是 listCases 或 listPersons。

My InvocationResult class is generic and contains a String returnCode attribute as well as an object reference to the object I would like to be converted to JSON. This object will be different depending on whether listCases or listPersons is executed.

我的 InvocationResult 类是通用的,包含一个 String returnCode 属性以及对我想要转换为 JSON 的对象的对象引用。根据执行的是 listCases 还是 listPersons,此对象会有所不同。

Thanks,

谢谢,

Bic

比克

采纳答案by Cy?egha

My impression is that your actual issue isn't about marshalling (which should be entirely straightforward), but about processing a response after having routed the message using choice(). You need to close the choice()block using end()(assuming the result of each branch will be processed in the same way), then make sure the response gets written to the outmessage body in the last step of the route.

我的印象是您的实际问题不在于编组(这应该是完全简单的),而是关于在使用choice(). 您需要使用关闭choice()end()(假设每个分支的结果将以相同的方式处理),然后确保out在路由的最后一步将响应写入消息正文。

Anyway, here is an example I've just tested:

无论如何,这是我刚刚测试过的一个例子:

public class HymansonTestRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty:http://localhost:8181/foo").to("direct:foo");

        from("direct:foo")
        .unmarshal().json(JsonLibrary.Hymanson, Foo.class)
        .choice()
            .when().simple("${body.foo} == 'toto'")
                .log("sending to beanA")
                .to("bean:beanA")
            .otherwise()
                .log("sending to beanB")
                .to("bean:beanB")
        // close the choice() block :
        .end()
        // per the javadoc for marshall(), "the output will be added to the out body" :
        .marshal().json(JsonLibrary.Hymanson);
    }
}

public class Foo {
    private String foo; // Constructor and accessor omitted for brevity
}

public class Bar1 {
    private String bar1; // Constructor and accessor omitted for brevity
}

public class Bar2 {
    private String bar2; // Constructor and accessor omitted for brevity
}

public class BeanA {
    public Bar1 doSomething(final Foo arg) {
        return new Bar1(arg.getFoo() + "A");
    }
}

public class BeanB {
    public Bar2 doSomething(final Foo arg) {
        return new Bar2(arg.getFoo() + "B");
    }
}

POSTing {"foo":"toto"}returns {"bar1":"totoA"}(and logs sending to beanA).

POSTing{"foo":"toto"}返回{"bar1":"totoA"}(和日志sending to beanA)。

POSTing {"foo":"titi"}returns {"bar2":"titiB"}(and logs sending to beanB).

POSTing{"foo":"titi"}返回{"bar2":"titiB"}(和日志sending to beanB)。

回答by Sagar

It is as simple as this .marshal().json(JsonLibrary.Hymanson)(this is what you want)

就这么简单.marshal().json(JsonLibrary.Hymanson)(这就是你想要的)