使用spring xml dsl定义时,camel-jaxb无法解编集xml

0pizxfdo  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(149)

我正在使用camel-jaxb组件来解压缩Artemis队列中的xml,但是我的本地jaxb无法找到jaxb.index文件。
我调试并注意到,ContextFactory.javaloadIndexedClasses().
在本地,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
gojuced7

gojuced71#

我必须创建jaxb类并使用该contextpath
第一步:

  • 使用xml创建xsd(在本例中,我使用了Intellij IDE社区版)

第二步:

  • 已将xs:schema更新为xsd:schema,因为这是cfx-xjc插件所使用的。

第三步:

  • pom.xml中更新
  • 我在项目级别下创建了文件夹模式,并将simpleOrder.xsd
<plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-xjc-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <extensions>
            <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:3.2.3</extension>
          </extensions>
        </configuration>
        <executions>
          <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>xsdtojava</goal>
            </goals>
            <configuration>
              <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
              <xsdOptions>
                <xsdOption>
                  <xsd>${basedir}/schema/simpleOrder.xsd</xsd>
                </xsdOption>
              </xsdOptions>
            </configuration>
          </execution>
        </executions>
      </plugin>

第四步:

  • 运行mvn:install在指定的位置生成源代码。注意,会有编译错误,我忽略了它。
  • 将生成的文件复制到org.example.dataformat下,其中intern更新了类的包。

注意事项:

  • 默认情况下,ObjectFactory.java和MessageType.java(在我的例子中)在包中使用jakarta.xml,我需要用javax.xml替换它。

请参阅下面的INFO,由于在JAXB类中使用jakarta.xml包,因此在使用camel-jaxb解组期间出现异常。
第五步:

  • 最后运行MainSpringContextApp.java启动路由,没有任何异常。
    信息
  • 如果我在JAXB类上使用jakarata.xml包,我需要添加依赖jakarta.xml绑定API来支持jaxb类
<dependency>
      <groupId>jakarta.xml.bind</groupId>
      <artifactId>jakarta.xml.bind-api</artifactId>
      <version>3.0.1</version>
 </dependency>
  • 用上面的方法当我接收xml输入的时候,下面的异常消息被显示出来。所以不得不使用javax.xml
java.io.IOException: javax.xml.bind.UnmarshalException
 - with linked exception:
[com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 1; unexpected element (uri:"", local:"message"). Expected elements are (none)]
    at org.apache.camel.converter.jaxb.JaxbDataFormat.unmarshal(JaxbDataFormat.java:308) ~[camel-jaxb-3.17.0.jar:3.17.0]
    at org.apache.camel.support.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:76) [camel-support-3.17.0.jar:3.17.0]
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$SimpleTask.run(RedeliveryErrorHandler.java:471) [camel-core-processor-3.17.0.jar:3.17.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:189) [camel-base-engine-3.17.0.jar:3.17.0]
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:61) [camel-base-engine-3.17.0.jar:3.17.0]
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:184) [camel-core-processor-3.17.0.jar:3.17.0]
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:399) [camel-base-engine-3.17.0.jar:3.17.0]
    at org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:269) [camel-seda-3.17.0.jar:3.17.0]
    at org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:187) [camel-seda-3.17.0.jar:3.17.0]
    at org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:130) [camel-seda-3.17.0.jar:3.17.0]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: javax.xml.bind.UnmarshalException
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:453) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:387) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:356) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at org.apache.camel.converter.jaxb.JaxbDataFormat.unmarshal(JaxbDataFormat.java:300) ~[camel-jaxb-3.17.0.jar:3.17.0]
    ... 12 more
Caused by: com.sun.istack.SAXParseException2: unexpected element (uri:"", local:"message"). Expected elements are (none)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:712) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:232) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:227) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:94) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1117) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:542) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:524) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:216) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:150) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:385) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:356) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at org.apache.camel.converter.jaxb.JaxbDataFormat.unmarshal(JaxbDataFormat.java:300) ~[camel-jaxb-3.17.0.jar:3.17.0]
    ... 12 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"message"). Expected elements are (none)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:712) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:232) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:227) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:94) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1117) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:542) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:524) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:216) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:150) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:385) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:356) ~[jaxb-impl-2.3.3.jar:2.3.3]
    at org.apache.camel.converter.jaxb.JaxbDataFormat.unmarshal(JaxbDataFormat.java:300) ~[camel-jaxb-3.17.0.jar:3.17.0]
    ... 12 more

相关问题