Apache Camel 4.0.4运行时加载XML路由报SAXException错误求助
问题:Apache Camel 4.0.4 运行时加载XML路由报Schema验证错误
环境与代码
环境:Apache Camel 4.0.4、JDK17,未使用Spring Boot。
运行时加载路由的代码:
var routesLoader = camelContext.getCamelContextExtension().getContextPlugin(RoutesLoader.class); Resource resource = ResourceHelper.fromBytes(path, xml.getBytes()); routesLoader.loadRoutes(resource);
测试用XML字符串:
private static String camelValidStr = "<?xml version='1.0' encoding='UTF-8'?>" + " <beans " +" xmlns='http://www.springframework.org/schema/beans' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +" xsi:schemaLocation='" +" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" +" http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd'" +" >" +" <camelContext id='camel' xmlns='http://camel.apache.org/schema/spring'>" + " </camelContext>" +" </beans>";
错误信息
org.xml.sax.SAXException: Line: 1, Column: 478, error: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'camelContext'.
解决建议
- 确认依赖完整性:你使用的是Spring风格的Camel XML配置,必须确保
camel-spring依赖已引入,版本需与Camel核心一致(4.0.4)。Maven依赖示例:<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>4.0.4</version> </dependency> - 改用Camel原生XML路由格式:既然未使用Spring Boot,推荐使用无需Spring容器的纯Camel路由格式,避免Spring相关依赖。示例XML:
<?xml version='1.0' encoding='UTF-8'?> <routes xmlns="http://camel.apache.org/schema/spring"> <!-- 可在此添加具体路由节点 --> <route id="sampleRoute"> <from uri="direct:start"/> <to uri="log:output"/> </route> </routes> - 检查Schema加载配置:如果坚持使用Spring格式,可配置本地Schema文件来替代网络加载,避免因网络或Schema地址变更导致的验证失败。
- 验证RoutesLoader的适配性:使用原生Camel路由格式时,RoutesLoader可直接加载路由内容,无需外层的
<beans>包裹,更符合无Spring环境的使用场景。
内容的提问来源于stack exchange,提问作者mszev
相关产品推荐
相关产品推荐

