Camel REST Bean Chaining

别等时光非礼了梦想. 提交于 2019-12-13 14:23:48

问题


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

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.

How can I do this using Camel?


回答1:


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).

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


来源:https://stackoverflow.com/questions/32565267/camel-rest-bean-chaining

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!