首页 > 解决方案 > 在 onException().onWhen() 中使用 bean

问题描述

我正在使用这本书,Camel in Action 2nd Edition。它有一个示例,我正在使用 aa 指南来捕获 http 错误并确定 http 状态代码是什么。但是,我收到一个错误,表明没有“bean”方法。

该示例可在此处获得,部分勘误表 (p510) https://raw.githubusercontent.com/camelinaction/camelinaction2/master/errata.txt

顺便说一句,勘误表上描述的错误不是我的问题。当我在 onWhen() 中有 bean() 时,我无法编译代码。

我在这里做错了什么?

电子邮件路由器.java

.doTry()
    .log("Initial Header: ${headers.Authorization} ${body}")
    .to("https://test.net/rest/api/email")
.doCatch(HttpOperationFailedException.class)
    .onWhen(bean(FailureBean.class, "httpAuthFail"))  // Causes "The method bean(Class<FailureBean>, String) is undefined for the type EmailRouter"
    .log("Before InOnly: ${headers.Authorization} ${body}")
    .to("direct:dead?exchangePattern=InOnly")
    .setBody(simple("${headers.MessageBody}"))
    .setHeader("Authorization", exchangeProperty("token"))
    .log("Newly Set Header: ${headers.Authorization} ${body}")
    .to("https://test.net/rest/api/email")
.end()

FailureBean.java

package com.bw.beans;

import java.util.Map;

import org.apache.camel.ExchangeException;
import org.apache.camel.Headers;
import org.apache.camel.http.base.HttpOperationFailedException;

public class FailureBean {

    public static boolean httpAuthFail(@ExchangeException HttpOperationFailedException cause) {
        int code = cause.getStatusCode();
        if (code == 401) {
            return true;
        }
        else {
            return false; 
        }
    }
}

标签: spring-bootapache-camel

解决方案


bean选项已从Camel 3 中的 Camels BeanLanguage中删除。您可以在Camel 2.x 文档中找到它(已弃用)。

这仅仅意味着,而不是

.onWhen(bean(FailureBean.class, "httpAuthFail"))

必须使用该method选项

.onWhen(method(FailureBean.class, "httpAuthFail"))

Camel in Action第 2 版在 Camel 3.x 之前发布,因此一些代码示例必须针对 Camel 3.x进行调整。Camel 迁移指南对此非常有帮助。


推荐阅读