我正在使用camel-jaxb组件来解压缩Artemis队列中的xml,但是我的本地jaxb无法找到jaxb.index
文件。
我调试并注意到,ContextFactory.java
,loadIndexedClasses()
.
在本地,resourceAsStream
为空,尽管在本例中路径是正确的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;
}
- 例外消息
Caused by: org.apache.camel.RuntimeCamelException: javax.xml.bind.JAXBException: "org.example.dataformat" doesnt contain ObjectFactory.class or jaxb.index
项目配置中是否存在任何问题?
下面是使用camel java maven构件构建的完整代码。
第一个
- 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
1条答案
按热度按时间gojuced71#
我必须创建jaxb类并使用该contextpath
第一步:
第二步:
cfx-xjc
插件所使用的。第三步:
pom.xml
中更新simpleOrder.xsd
第四步:
mvn:install
在指定的位置生成源代码。注意,会有编译错误,我忽略了它。org.example.dataformat
下,其中intern更新了类的包。注意事项:
javax.xml
替换它。请参阅下面的INFO,由于在JAXB类中使用
jakarta.xml
包,因此在使用camel-jaxb解组期间出现异常。第五步:
MainSpringContextApp.java
启动路由,没有任何异常。信息
jakarata.xml
包,我需要添加依赖jakarta.xml
绑定API来支持jaxb类javax.xml
。