使用Spring XML DSL定义时,Camel-Jaxb无法解散XML
我正在使用Camel-Jaxb组件来取消Artemis队列中的XML XML,但是我本地的Jaxb无法找到Jaxb.Index
file。
我调试并注意到,contextFactory.java
,loadIndexedClasses()
。
本地的resourceasstream
是零,尽管在这种情况下该路径是正确的org/example/dateformat/jaxb.index
private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException {
final String resource = pkg.replace('.', '/') + "/jaxb.index";
final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);
if (resourceAsStream == null) {
return null;
}
- 例外消息
Caused by: org.apache.camel.RuntimeCamelException: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
中是否有项目配置中有任何ISSIS?
以下是使用骆驼Java Maven工件的完整代码构建。
<?xml version="1.0" encoding="UTF-8"?>
<message id="1">
<to>ORDER_DEP</to>
<from>customer1</from>
<status>APPROVED</status>
<description>validated</description>
</message>
<?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/aop
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">
<bean id="jmsConnectionFactory" class="org.apache.qpid.jms.JmsConnectionFactory" >
<property name="username" value="admin"/>
<property name="password" value="admin"/>
<property name="remoteURI" value="amqp://localhost:5672" />
</bean>
<bean id="jmsPooledConnectionFactory" class="org.messaginghub.pooled.jms.JmsPoolConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="5" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="jmsPooledConnectionFactory" />
<property name="concurrentConsumers" value="5" />
</bean>
<!-- using the AMQP component -->
<bean id="jms" class="org.apache.camel.component.amqp.AMQPComponent">
<property name="configuration" ref="jmsConfig" />
<property name="connectionFactory" ref="jmsPooledConnectionFactory"/>
</bean>
<bean id="demoBean" class="org.example.Demo"></bean>
<camelContext id="testConsumer" xmlns="http://camel.apache.org/schema/spring" autoStartup="true" allowUseOriginalMessage="true">
<packageScan>
<package>org.example.dataformat</package>
</packageScan>
<!-- not able to create the ref in the dataformats.
<dataFormats >
<jaxb id="customConverter" contextPath="org.example.dataformat" filterNonXmlChars="true"/>
</dataFormats>
-->
<route id="testroute" autoStartup="true">
<from uri="jms:queue:content_queue?selector=messageType LIKE 'ORDER%'"/>
<log loggingLevel="DEBUG" message="info ${header.fileName}"/>
<unmarshal>
<zipFile/>
</unmarshal>
<choice>
<when>
<simple>${header.messageType} == 'ORDER' </simple>
<wireTap uri="file:data/output/consumed/"/>
<to uri="seda:processOrder"/>
</when>
</choice>
</route>
<route id="processFile" autoStartup="true">
<from uri="seda:processOrder"/>
<unmarshal allowNullBody="true">
<jaxb prettyPrint="true" contextPath="org.example.dataformat" filterNonXmlChars="true"/>
</unmarshal>
<bean ref="org.example.Demo" method="processOrder"/>
</route>
</camelContext>
</beans>
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SimpleCamelConsumer</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>A Camel Route</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<log4j2-version>2.13.3</log4j2-version>
<pooled-jms-version>2.0.5</pooled-jms-version>
<qpid-jms-client-version>1.6.0</qpid-jms-client-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>3.17.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-xml</artifactId>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>${pooled-jms-version}</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>${qpid-jms-client-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-zipfile</artifactId>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
<version>${log4j2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>3.17.0</version>
<configuration>
<logClasspath>true</logClasspath>
<mainClass>org.example.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
- demo.java
package org.example;
import org.example.dataformat.Message;
public class Demo {
public void processOrder(Message message){
System.out.println("message value "+message.getId()+" "+message.getFrom());
}
}
- message.java
package org.example.dataformat;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Message {
@XmlAttribute
Integer id;
@XmlElement(name="to")
String to;
@XmlElement(name="from")
String from;
@XmlElement(name="status")
String status;
@XmlElement(name="description")
String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
- jaxb.index
Message
- mainspringcontextapp.java
package org.example;
import org.apache.camel.spring.Main;
public class MainSpringContextApp {
public static void main(String... args) throws Exception {
Main main = new Main();
main.run(args);
}
}
- 当我在Intellij Idea Community IDE中执行代码时,我在以下以下
Exception in thread "main" org.apache.camel.FailedToStartRouteException: Failed to start route processFile because of null
at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:123)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doWarmUpRoutes(InternalRouteStartupManager.java:306)
at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:189)
...
Caused by: org.apache.camel.RuntimeCamelException: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
at org.apache.camel.RuntimeCamelException.wrapRuntimeException(RuntimeCamelException.java:66)
at org.apache.camel.support.service.BaseService.doFail(BaseService.java:413)
at org.apache.camel.support.service.BaseService.fail(BaseService.java:342)
at org.apache.camel.support.service.BaseService.start(BaseService.java:132)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
...
Caused by: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:260)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:48)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:302)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:478)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:435)
at org.apache.camel.converter.jaxb.JaxbDataFormat.createContext(JaxbDataFormat.java:552)
at org.apache.camel.converter.jaxb.JaxbDataFormat.doStart(JaxbDataFormat.java:516)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
... 53 more
I am using the camel-jaxb component to unmarshall xml zipped in the Artemis queue, but jaxb in my local is not able to find the jaxb.index
file.
I debugged and noticed, the ContextFactory.java
, loadIndexedClasses()
.
Locally the resourceAsStream
is null, though the path is correct in this case org/example/dataformat/jaxb.index
private static List<Class> loadIndexedClasses(String pkg, ClassLoader classLoader) throws IOException, JAXBException {
final String resource = pkg.replace('.', '/') + "/jaxb.index";
final InputStream resourceAsStream = classLoader.getResourceAsStream(resource);
if (resourceAsStream == null) {
return null;
}
- Exception message
Caused by: org.apache.camel.RuntimeCamelException: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
Is there any issuses in the project configuration?
Below is the complete code build using the camel java maven artifact.
<?xml version="1.0" encoding="UTF-8"?>
<message id="1">
<to>ORDER_DEP</to>
<from>customer1</from>
<status>APPROVED</status>
<description>validated</description>
</message>
<?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/aop
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">
<bean id="jmsConnectionFactory" class="org.apache.qpid.jms.JmsConnectionFactory" >
<property name="username" value="admin"/>
<property name="password" value="admin"/>
<property name="remoteURI" value="amqp://localhost:5672" />
</bean>
<bean id="jmsPooledConnectionFactory" class="org.messaginghub.pooled.jms.JmsPoolConnectionFactory" init-method="start" destroy-method="stop">
<property name="maxConnections" value="5" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="jmsPooledConnectionFactory" />
<property name="concurrentConsumers" value="5" />
</bean>
<!-- using the AMQP component -->
<bean id="jms" class="org.apache.camel.component.amqp.AMQPComponent">
<property name="configuration" ref="jmsConfig" />
<property name="connectionFactory" ref="jmsPooledConnectionFactory"/>
</bean>
<bean id="demoBean" class="org.example.Demo"></bean>
<camelContext id="testConsumer" xmlns="http://camel.apache.org/schema/spring" autoStartup="true" allowUseOriginalMessage="true">
<packageScan>
<package>org.example.dataformat</package>
</packageScan>
<!-- not able to create the ref in the dataformats.
<dataFormats >
<jaxb id="customConverter" contextPath="org.example.dataformat" filterNonXmlChars="true"/>
</dataFormats>
-->
<route id="testroute" autoStartup="true">
<from uri="jms:queue:content_queue?selector=messageType LIKE 'ORDER%'"/>
<log loggingLevel="DEBUG" message="info ${header.fileName}"/>
<unmarshal>
<zipFile/>
</unmarshal>
<choice>
<when>
<simple>${header.messageType} == 'ORDER' </simple>
<wireTap uri="file:data/output/consumed/"/>
<to uri="seda:processOrder"/>
</when>
</choice>
</route>
<route id="processFile" autoStartup="true">
<from uri="seda:processOrder"/>
<unmarshal allowNullBody="true">
<jaxb prettyPrint="true" contextPath="org.example.dataformat" filterNonXmlChars="true"/>
</unmarshal>
<bean ref="org.example.Demo" method="processOrder"/>
</route>
</camelContext>
</beans>
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SimpleCamelConsumer</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>A Camel Route</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<log4j2-version>2.13.3</log4j2-version>
<pooled-jms-version>2.0.5</pooled-jms-version>
<qpid-jms-client-version>1.6.0</qpid-jms-client-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>3.17.0</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-main</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-xml</artifactId>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
<version>${pooled-jms-version}</version>
</dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>${qpid-jms-client-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xstream</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-zipfile</artifactId>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
<version>${log4j2-version}</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>3.17.0</version>
<configuration>
<logClasspath>true</logClasspath>
<mainClass>org.example.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
- Demo.java
package org.example;
import org.example.dataformat.Message;
public class Demo {
public void processOrder(Message message){
System.out.println("message value "+message.getId()+" "+message.getFrom());
}
}
- Message.java
package org.example.dataformat;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Message {
@XmlAttribute
Integer id;
@XmlElement(name="to")
String to;
@XmlElement(name="from")
String from;
@XmlElement(name="status")
String status;
@XmlElement(name="description")
String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
- jaxb.index
Message
- MainSpringContextApp.java
package org.example;
import org.apache.camel.spring.Main;
public class MainSpringContextApp {
public static void main(String... args) throws Exception {
Main main = new Main();
main.run(args);
}
}
- When i execute the code in Intellij Idea Community IDE, I get below exception
Exception in thread "main" org.apache.camel.FailedToStartRouteException: Failed to start route processFile because of null
at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:123)
at org.apache.camel.impl.engine.InternalRouteStartupManager.doWarmUpRoutes(InternalRouteStartupManager.java:306)
at org.apache.camel.impl.engine.InternalRouteStartupManager.safelyStartRouteServices(InternalRouteStartupManager.java:189)
...
Caused by: org.apache.camel.RuntimeCamelException: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
at org.apache.camel.RuntimeCamelException.wrapRuntimeException(RuntimeCamelException.java:66)
at org.apache.camel.support.service.BaseService.doFail(BaseService.java:413)
at org.apache.camel.support.service.BaseService.fail(BaseService.java:342)
at org.apache.camel.support.service.BaseService.start(BaseService.java:132)
at org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
...
Caused by: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:260)
at com.sun.xml.bind.v2.JAXBContextFactory.createContext(JAXBContextFactory.java:48)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:302)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:478)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:435)
at org.apache.camel.converter.jaxb.JaxbDataFormat.createContext(JaxbDataFormat.java:552)
at org.apache.camel.converter.jaxb.JaxbDataFormat.doStart(JaxbDataFormat.java:516)
at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
... 53 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须创建JAXB类,并使用该上下文路径
步骤1:
步骤2:
cfx-xjc
插件使用。步骤3:
pom.xml
simpleordor.xsd
step 4:
运行
MVN:安装
在指定的位置下生成了源。请注意,将出现汇编错误,我忽略了它。在
org.example.dataformat下复制生成的文件
实习生更新了类的软件包。注意:
javax.xml
替换。请参阅下面的 info ,因为在Jaxb类中使用
jakarta.xml
软件包,因此在使用Camel-Jaxb进行删除时,有一个例外。步骤5:
mainspringContextapp.java
启动了路由而无需任何例外。info
jakarata.xml
jaxb类上的软件包,我需要添加依赖项jakarta.xml
javax.xml
。I had to create the jaxb class and use that contextpath
Step 1:
Step 2:
cfx-xjc
plugin uses.Step 3:
pom.xml
update thesimpleOrder.xsd
Step 4:
Running the
mvn:install
generated the source under the specified location. Note, there will be a compilation error, I ignored it.Copied the generated file under the
org.example.dataformat
which intern updates the package of the class.Note:
javax.xml
.Refer INFO below, since using
jakarta.xml
package in the JAXB classes, there is an exception during unmarshalling with camel-jaxb.Step 5:
MainSpringContextApp.java
started the route without any exception.INFO
jakarata.xml
package on the JAXB classes, I need to add dependencyjakarta.xml
bind api to support jaxb classesjavax.xml
.