spring-boot - 在 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;
}
}
}
解决方案
该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 迁移指南对此非常有帮助。
推荐阅读
- php - 真实服务器上邮件驱动程序的laravel邮件发送问题
- c - 使用结构和数组的错误 int 值
- c# - 如果外部匹配,正则表达式选择内部
- android - 我的 XML 文件更改自动包含另一个标签
- spring - Kafka 监听器接收列表
>,可以食用吗? - python - 如何使用 ElementTree 在 xml 文件中搜索标签,其中我有一个具有特定值的特定“父”标签?(Python)
- angular - 在 Angular 项目中找不到带有 https 连接的页面
- r - 使用 ggplot() 中的函数 sprintf() 将条形图中的单个条形标记为 3 个有效数字
- google-cloud-platform - 无法连接到 GCP 中的 SQL 云实例
- angular - 将数据提取到 Angular 表单数组