如何在运行时切换绑定(Camel Rest Dsl)

时间:2019-03-26 07:57:31

标签: java apache-camel

我有一个绑定模式设置为off的rest端点:

rest("/users")
   .bindingMode(RestBindingMode.off)
      .get()
      .route()
      .id("Get all users")
        .bean(userService,"getAll")
.endRest()

我想在捕获到异常时将绑定模式切换为json:

 onException(UserNotFoundException.class)
   .handled(true)
   .process(responseProcessor);

如何用骆驼做到这一点?我尝试将标头CamelBinding设置为json,但这不起作用。还有其他想法吗?

2 个答案:

答案 0 :(得分:2)

您不能那样做。在onException的处理器中,您可以自己将响应有效载荷设置为JSon。如果需要,您可以使用Camel的dataformat / components来获得JSon支持。

答案 1 :(得分:0)

您可以尝试使用以下命令在处理方法中更改绑定模式:

@Override
    public void process(Exchange exchange) throws Exception {
exchange.getContext().getRestConfiguration().setBindingMode(RestConfiguration.RestBindingMode.json);
}