我正在测试一个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为null我正在通过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;
}
我尝试构造函数注入JaxbDataFormat对象来创建DataFormatDefinition类实例(因此DataFormat不为null)但是我仍然得到相同的错误,指出在注册表中找不到DataFormat。
有人可以帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:1)
它不是你应该在JNDI中创建和绑定的模型类,而是来自camel-jaxb的真正的DataFormat类。它们具有相同的类名,但包名称不同。