我正在测试一种骆驼路线,该路线将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


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

我正在遍历DataFormatDefinition.java(下面给出)的代码。调试时,我发现Routecontext.getcamelContext()下的jaxbcontext为空。

   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。

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

最佳答案

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

10-06 14:42