简体   繁体   English

骆驼 REST Bean Chaining

[英]Camel REST Bean Chaining

I currently have a REST route builder that looks as follows:我目前有一个 REST 路由生成器,如下所示:

rest("/v1")
  .post("/create")
    .to("bean:myAssembler?method=assemble(${in.header.content})")
    .to("bean:myService?method=create(?)");

The bean myAssembler takes raw JSON and transforms this into MyObject. bean myAssembler 获取原始 JSON 并将其转换为 MyObject。 This object is then returned and I want it forwarded onto myService as a parameter for its create method.然后返回这个 object,我希望它作为 create 方法的参数转发到 myService 上。

How can I do this using Camel?我如何使用骆驼来做到这一点?

Your beans will bind automatically to specific parameters like Exchange if you put it as a parameter to a method (see complete list Parameter binding ). 如果将bean作为参数添加到方法中,则bean将自动绑定到特定参数(如Exchange)(请参阅完整列表参数绑定 )。

One solution would be to define your route and beans like this: 一种解决方案是定义您的路线和豆类如下:

restConfiguration()
.component("restlet")
.bindingMode(RestBindingMode.json)
.skipBindingOnErrorCode(false)
.port(port);    

rest("/v1")
    .post("/create")
        .route()
            .to("bean:myAssembler?method=assemble")
            .to("bean:myService?method=create");

with beans like this 像这样的豆子

public class MyAssembler {
    public void assemble(Exchange exchange) {
      String content = exchange.getIn().getHeader("content", String.class);
      // Create MyObject here.
      MyObject object; // ...transformation here.
      exchange.getOut().setBody(object);
   }
}

and this 还有这个

public class MyService {
    public void create(MyObject body) {
        // Do what ever you want with the content.
        // Here it's just log.
        LOG.info("MyObject is: " + body.toString());
     }
}

The dependencies for shown configuration are 显示配置的依赖关系是

org.apache.camel/camel-core/2.15.3
org.apache.camel/camel-spring/2.15.3
org.apache.camel/camel-restlet/2.15.3
javax.servlet/javax.servlet-api/3.1.0
org.apache.camel/camel-jackson/2.15.3
org.apache.camel/camel-xmljson/2.15.3
xom/xom/1.2.5

Actually, if last bean returns MyObject , next bean can accept and bind MyObject as first arg.实际上,如果最后一个 bean 返回MyObject ,下一个 bean 可以接受并绑定MyObject作为第一个参数。 You don't need to put it into Exchange body or anything.您不需要将其放入 Exchange 正文或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM