Camel REST Bean 链接 [英] Camel REST Bean Chaining

查看:21
本文介绍了Camel REST Bean 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 REST 路由构建器,如下所示:

I currently have a REST route builder that looks as follows:

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

bean myAssembler 获取原始 JSON 并将其转换为 MyObject.然后返回此对象,我希望将其作为其 create 方法的参数转发到 myService.

The bean myAssembler takes raw JSON and transforms this into MyObject. This object is then returned and I want it forwarded onto myService as a parameter for its create method.

如何使用 Camel 执行此操作?

How can I do this using Camel?

推荐答案

如果你把它作为一个方法的参数,你的 bean 将自动绑定到特定的参数,比如 Exchange(参见完整列表 参数绑定).

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:

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");

像这样的豆子

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);
   }
}

还有这个

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());
     }
}

显示配置的依赖项是

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

这篇关于Camel REST Bean 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆