为了说明我的问题,我创建了一个小的spring boot示例应用程序。该应用程序的目的是创建一个Jaxb2Marshaller
bean。
@SpringBootApplication
public class App implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller bean = new Jaxb2Marshaller();
bean.setContextPath("ch.sahits.game.helloworld");
return bean;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Started up");
}
}
此代码无法启动,但例外情况为:
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:174)
... 23 more
我对此异常的理解是为什么试图com.sun.xml.internal.bind.v2.ContextFactory
实例化? It has been suggested,我还需要运行时的依赖项,所以我补充说:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
但是这只会让我有一个不同的例外,试图加载一个不同的类:
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.model.annotation.AnnotationReader
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 39 more
以下是完整的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.sahits.game</groupId>
<artifactId>AddModuleDependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<cxf-xjc-plugin.version>2.3.0</cxf-xjc-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-xjc-plugin</artifactId>
<version>${cxf-xjc-plugin.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>xsdtojava</goal>
</goals>
<configuration>
<extensions>
<extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${cxf-xjc-plugin.version}</extension>
</extensions>
<xsdOptions>
<xsdOption>
<xsd>src/main/resources/helloworld.xsd</xsd>
<bindingFile>src/main/resources/jaxb-binding.xjb</bindingFile>
<packagename>ch.sahits.game.helloworld</packagename>
</xsdOption>
</xsdOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
当然,简单的解决方案是添加--add-modules java.xml.ws
作为VM选项,但这正是我试图摆脱的,以使我的应用程序未来证明。
我必须使用什么依赖来解决此问题?或者我是否更改了bean配置,以便查找正确的类(而不是com.sun.xml...
中的类)?
download的MCVE作为zip存档。
答案 0 :(得分:4)
尝试添加以下内容:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
jaxb-core
包含com.sun.xml.bind.v2.model.annotation.AnnotationReader
(似乎是jaxb-runtime
的必需依赖项,至少在您的情况下),javax.activation
需要jaxb-api
由于后者使用DataHandler
。
此外,没有单个bean类,因此编组器将无法初始化。我添加了以下内容
@XmlRootElement
public class MyBean {
}
并替换
bean.setContextPath("ch.sahits.game.helloworld");
与
bean.setClassesToBeBound(MyBean.class);
之后应用程序已经开始。