bic*_*ter 8 json marshalling apache-camel unmarshalling
我想我有一个简单的问题,但似乎无法弄明白.
我正在使用从解组JSON创建的类调用POJO作为方法的参数.问题是,如何将方法的返回编组回JSON?
我的路线在下面;
from("direct:start")
 .choice()
  .when(header("methodname").isEqualTo("listCases"))
   .unmarshal().json(JsonLibrary.Jackson, UserDetails.class)
   .to("bean:com.xxx.BeanA")
  .when(header("methodName").isEqualTo("listPersons"))
   .unmarshal().json(JsonLibrary.Jackson, CaseDetails.class)
   .to("bean:com.xxx.BeanB");
......我正在通过下面的方式调用路线;
ProducerTemplate template = camelContext.createProducerTemplate();
template.setDefaultEndpoint(camelContext.getEndpoint("direct:start"));
InvocationResult result = (InvocationResult)template.requestBodyAndHeader(payload, "methodName", methodName);
Payload是JSON,在此示例中,methodName是listCases或listPersons.
我的InvocationResult类是通用的,包含String returnCode属性以及对我想要转换为JSON的对象的对象引用.此对象将根据是否执行listCases或listPersons而有所不同.
谢谢,
比克
我的印象是你的实际问题不是关于编组(这应该是完全直截了当的),而是关于在使用路由消息后处理响应choice().您需要使用关闭choice()块end()(假设每个分支的结果将以相同的方式处理),然后确保响应out在路由的最后一步写入消息体.
无论如何,这是我刚刚测试过的一个例子:
public class JacksonTestRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("jetty:http://localhost:8181/foo").to("direct:foo");
        from("direct:foo")
        .unmarshal().json(JsonLibrary.Jackson, 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.Jackson);
    }
}
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");
    }
}
POST {"foo":"toto"}返回{"bar1":"totoA"}(和日志sending to beanA).
POST {"foo":"titi"}返回{"bar2":"titiB"}(和日志sending to beanB).
| 归档时间: | 
 | 
| 查看次数: | 20084 次 | 
| 最近记录: |