gpt4 book ai didi

java - Camel路由测试错误: IllegalArgumentException: Data format 'jaxb' could not be created

转载 作者:行者123 更新时间:2023-12-02 12:04:45 41 4
gpt4 key购买 nike

我正在测试一个camel路由,它将xml(字符串形式)有效负载解码到jaxb生成的bean,然后进一步在处理器中使用的属性中设置bean。整个事情在实际流程中完美运行,但是当我尝试运行 junit 来测试我的路线时,我收到错误:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 6.536 sec <<< FAILURE! - in com.equifax.icc.esb.tradinghistory.routes.report.PreDataRetrievalServiceTest
preDataRetrievalOrchestrationSuccessTest(com.equifax.icc.esb.tradinghistory.routes.report.PreDataRetrievalServiceTest) Time elapsed: 4.575 sec <<< ERROR!
org.apache.camel.FailedToCreateRouteException: Failed to create route report-info-preparation-service-route at: >>> Unmarshal[ref:bthRequestModel] <<< in route: Route(report-info-preparation-service-route)[[From[direct:re... because of Data format 'jaxb' could not be created. Ensure that the data format is valid and the associated Camel component is present on the classpath
Caused by: java.lang.IllegalArgumentException: Data format 'jaxb' could not be created. Ensure that the data format is valid and the associated Camel component is present on the class path:

我已经重写了 getBlueprintDescriptor() 并包含了所有蓝图上下文文件(包括具有 JAXB DataFormat 的 bean 声明的文件)。以下是 getBlueprintDescriptor() 方法和 bean 声明:

@Override
protected String getBlueprintDescriptor() {
return "/OSGI-INF/blueprint/test-beans-context.xml,"
+"/OSGI-INF/blueprint/test-env-context.xml,"
+ "/OSGI-INF/blueprint/test-camel-context.xml";
}
<小时/>
<bean class="org.apache.camel.model.dataformat.JaxbDataFormat" id="bthRequestModel">
<property name="prettyPrint" value="false" />
<property name="fragment" value="true" />
<property name="ignoreJAXBElement" value="true" />
<property name="contextPath" value="com.vedaxml.vxml2.veda_bth_request_v1" />
</bean>

我也尝试重写 createRegistry () :

@Override 
protected JndiRegistry createRegistry() throws Exception {
JndiRegistry registry = super.createRegistry();
JaxbDataFormat jdf = new org.apache.camel.model.dataformat.JaxbDataFormat(false);
jdf.setFragment(true);
jdf.setIgnoreJAXBElement(true);
jdf.setContextPath("com.vedaxml.vxml2.veda_bth_request_v1");

DataFormat jaxb = (DataFormat) jdf;

registry.bind( "bthRequestModel", jdf);
//registry.bind( "jaxb", new org.apache.camel.model.dataformat.JaxbDataFormat() );

return registry;
}

这给了我以下错误:

2017-10-27 22:38:56,613 INFO  org.apache.camel.test.junit4.CamelTestSupport  ********************************************************************************
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.792 sec <<< FAILURE! - in com.equifax.icc.esb.tradinghistory.routes.report.PreDataRetrievalServiceTest
preDataRetrievalOrchestrationSuccessTest(com.equifax.icc.esb.tradinghistory.routes.report.PreDataRetrievalServiceTest) Time elapsed: 4.316 sec <<< ERROR!
org.apache.camel.FailedToCreateRouteException: Failed to create route report-info-preparation-service-route at: >>> Unmarshal[ref:bthRequestModel] <<< in route: Route(report-info-preparation-service-route)[[From[direct:re... because of Cannot find data format in registry with ref: bthRequestModel
Caused by: java.lang.IllegalArgumentException: Cannot find data format in registry with ref: bthRequestModel

根据 https://issues.apache.org/jira/browse/CAMEL-3508 看来 dataFormat 为空

我正在浏览 DataFormatDefinition.java 的代码(如下所示)。在调试时,我观察到 Routecontext.getcamelContext() 下的 jaxbcontext 为 null。

   public static DataFormat getDataFormat(RouteContext routeContext, DataFormatDefinition type, String ref) {
if (type == null) {
ObjectHelper.notNull(ref, "ref or type");

// try to let resolver see if it can resolve it, its not always possible
type = routeContext.getCamelContext().resolveDataFormatDefinition(ref);

if (type != null) {
return type.getDataFormat(routeContext);
}

DataFormat dataFormat = routeContext.getCamelContext().resolveDataFormat(ref);
if (dataFormat == null) {
throw new IllegalArgumentException("Cannot find data format in registry with ref: " + ref);
}

return dataFormat;
} else {
return type.getDataFormat(routeContext);
}
}
<小时/>
public DataFormat getDataFormat(RouteContext routeContext) {
if (dataFormat == null) {
Runnable propertyPlaceholdersChangeReverter = ProcessorDefinitionHelper.createPropertyPlaceholdersChangeReverter();

// resolve properties before we create the data format
try {
ProcessorDefinitionHelper.resolvePropertyPlaceholders(routeContext.getCamelContext(), this);
} catch (Exception e) {
throw new IllegalArgumentException("Error resolving property placeholders on data format: " + this, e);
}
try {
dataFormat = createDataFormat(routeContext);
if (dataFormat != null) {
// is enabled by default so assume true if null
final boolean contentTypeHeader = this.contentTypeHeader == null || this.contentTypeHeader;
try {
setProperty(routeContext.getCamelContext(), dataFormat, "contentTypeHeader", contentTypeHeader);
} catch (Exception e) {
// ignore as this option is optional and not all data formats support this
}
// configure the rest of the options
configureDataFormat(dataFormat, routeContext.getCamelContext());
} else {
throw new IllegalArgumentException(
"Data format '" + (dataFormatName != null ? dataFormatName : "<null>") + "' could not be created. "
+ "Ensure that the data format is valid and the associated Camel component is present on the classpath");
}
} finally {
propertyPlaceholdersChangeReverter.run();
}
}
return dataFormat;
}

我尝试构造函数注入(inject) JaxbDataFormat 对象来创建 DataFormatDefinition 类实例(以便 DataFormat 不为 null),但仍然收到相同的错误,指出在注册表中找不到 DataFormat。

有人可以帮我解决这个问题吗?提前致谢。

最佳答案

它不是您应该在 JNDI 中创建和绑定(bind)的模型类,而是来自camel-jaxb 的真正 DataFormat 类。它们具有相同的类名,但包名不同。

关于java - Camel路由测试错误: IllegalArgumentException: Data format 'jaxb' could not be created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46974690/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com