我目前有一个REST路由构建器,如下所示:
rest("/v1")
.post("/create")
.to("bean:myAssembler?method=assemble(${in.header.content})")
.to("bean:myService?method=create(?)");
bean myAssembler接受原始JSON并将其转换为MyObject。然后返回此对象,我希望它作为其create方法的参数转发到myService。
如何使用Camel执行此操作?
答案 0 :(得分:3)
如果将bean作为参数添加到方法中,您的bean将自动绑定到Exchange等特定参数(请参阅完整列表Parameter binding)。
一种解决方案是定义您的路线和豆类:
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");
使用像这样的bean
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