我需要将我的应用程序从 Java 8 升级到 Java 17。我们有一些使用 JaxbContext 的解组器
JAXBContext jaxbContext = JAXBContext.newInstance(Order.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// Unmarshal the XML file into an Order object
Order order = (Order) jaxbUnmarshaller.unmarshal(file);
该方法在 Java 8 中工作正常,但在 Java 17 中,我将 order 中存在的成员设置为 null。我也没有得到任何例外。 注意:我尝试使用下面的依赖项,但它们没有帮助。我还有 EclipseLink 作为 Jpa 提供者。
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<!-- JAXB Runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>3.0.2</version>
</dependency>
<!-- Optional: If you are using Java 9 or above, you might need the following dependency -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
依赖项:单击
ADD DEPENDENCIES...
点击
GENERATE
保存演示.zip
解压演示.zip
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
运行命令
mvn dependency:tree
结果:
com.example:demo:jar:0.0.1-SNAPSHOT
+- org.springframework.boot:spring-boot-starter-jersey:jar:3.3.1:compile
| +- org.springframework.boot:spring-boot-starter-json:jar:3.3.1:compile
| | +- com.fasterxml.jackson.core:jackson-databind:jar:2.17.1:compile
| | | \- com.fasterxml.jackson.core:jackson-core:jar:2.17.1:compile
| | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.17.1:compile
| | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.17.1:compile
| | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.17.1:compile
| +- org.springframework.boot:spring-boot-starter-tomcat:jar:3.3.1:compile
| | +- jakarta.annotation:jakarta.annotation-api:jar:2.1.1:compile
| | +- org.apache.tomcat.embed:tomcat-embed-core:jar:10.1.25:compile
| | +- org.apache.tomcat.embed:tomcat-embed-el:jar:10.1.25:compile
| | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:10.1.25:compile
| +- org.springframework.boot:spring-boot-starter-validation:jar:3.3.1:compile
| | \- org.hibernate.validator:hibernate-validator:jar:8.0.1.Final:compile
| | \- com.fasterxml:classmate:jar:1.7.0:compile
| +- org.springframework:spring-web:jar:6.1.10:compile
| | +- org.springframework:spring-beans:jar:6.1.10:compile
| | \- io.micrometer:micrometer-observation:jar:1.13.1:compile
| | \- io.micrometer:micrometer-commons:jar:1.13.1:compile
| +- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:3.1.7:compile
| | +- jakarta.inject:jakarta.inject-api:jar:2.0.1:compile
| | +- org.glassfish.jersey.core:jersey-common:jar:3.1.7:compile
| | | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.3:compile
| | \- jakarta.ws.rs:jakarta.ws.rs-api:jar:3.1.0:compile
| +- org.glassfish.jersey.containers:jersey-container-servlet:jar:3.1.7:compile
| +- org.glassfish.jersey.core:jersey-server:jar:3.1.7:compile
| | +- org.glassfish.jersey.core:jersey-client:jar:3.1.7:compile
| | \- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile
| +- org.glassfish.jersey.ext:jersey-bean-validation:jar:3.1.7:compile
| | +- org.jboss.logging:jboss-logging:jar:3.5.3.Final:compile
| | \- org.glassfish.expressly:expressly:jar:5.0.0:compile
| +- org.glassfish.jersey.ext:jersey-spring6:jar:3.1.7:compile
| | +- org.glassfish.jersey.inject:jersey-hk2:jar:3.1.7:compile
| | | +- org.glassfish.hk2:hk2-locator:jar:3.0.6:compile
| | | | \- org.glassfish.hk2.external:aopalliance-repackaged:jar:3.0.6:compile
| | | \- org.javassist:javassist:jar:3.30.2-GA:compile
| | +- org.glassfish.hk2:hk2:jar:3.0.6:compile
| | | +- org.glassfish.hk2:hk2-utils:jar:3.0.6:compile
| | | +- org.glassfish.hk2:hk2-api:jar:3.0.6:compile
| | | +- org.glassfish.hk2:hk2-core:jar:3.0.6:compile
| | | +- org.glassfish.hk2:hk2-runlevel:jar:3.0.6:compile
| | | \- org.glassfish.hk2:class-model:jar:3.0.6:compile
| | | \- org.ow2.asm:asm-commons:jar:9.6:compile
| | \- org.glassfish.hk2:spring-bridge:jar:3.0.6:compile
| \- org.glassfish.jersey.media:jersey-media-json-jackson:jar:3.1.7:compile
| +- org.glassfish.jersey.ext:jersey-entity-filtering:jar:3.1.7:compile
| +- com.fasterxml.jackson.core:jackson-annotations:jar:2.17.1:compile
| \- com.fasterxml.jackson.module:jackson-module-jakarta-xmlbind-annotations:jar:2.17.1:compile
+- org.springframework.boot:spring-boot-starter-web:jar:3.3.1:compile
| +- org.springframework.boot:spring-boot-starter:jar:3.3.1:compile
| | +- org.springframework.boot:spring-boot:jar:3.3.1:compile
| | +- org.springframework.boot:spring-boot-autoconfigure:jar:3.3.1:compile
| | +- org.springframework.boot:spring-boot-starter-logging:jar:3.3.1:compile
| | | +- ch.qos.logback:logback-classic:jar:1.5.6:compile
| | | | \- ch.qos.logback:logback-core:jar:1.5.6:compile
| | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.23.1:compile
| | | | \- org.apache.logging.log4j:log4j-api:jar:2.23.1:compile
| | | \- org.slf4j:jul-to-slf4j:jar:2.0.13:compile
| | \- org.yaml:snakeyaml:jar:2.2:compile
| \- org.springframework:spring-webmvc:jar:6.1.10:compile
| +- org.springframework:spring-aop:jar:6.1.10:compile
| +- org.springframework:spring-context:jar:6.1.10:compile
| \- org.springframework:spring-expression:jar:6.1.10:compile
+- org.springframework.boot:spring-boot-starter-web-services:jar:3.3.1:compile
| +- com.sun.xml.messaging.saaj:saaj-impl:jar:3.0.4:compile
| | +- jakarta.xml.soap:jakarta.xml.soap-api:jar:3.0.2:compile
| | +- org.jvnet.staxex:stax-ex:jar:2.1.0:compile
| | +- jakarta.activation:jakarta.activation-api:jar:2.1.3:compile
| | \- org.eclipse.angus:angus-activation:jar:2.0.2:runtime
| +- jakarta.xml.ws:jakarta.xml.ws-api:jar:4.0.2:compile
| +- org.springframework:spring-oxm:jar:6.1.10:compile
| \- org.springframework.ws:spring-ws-core:jar:4.0.11:compile
| +- org.springframework.ws:spring-xml:jar:4.0.11:compile
| \- org.glassfish.jaxb:jaxb-runtime:jar:4.0.5:runtime
| \- org.glassfish.jaxb:jaxb-core:jar:4.0.5:runtime
| +- org.glassfish.jaxb:txw2:jar:4.0.5:runtime
| \- com.sun.istack:istack-commons-runtime:jar:4.1.2:runtime
\- org.springframework.boot:spring-boot-starter-test:jar:3.3.1:test
+- org.springframework.boot:spring-boot-test:jar:3.3.1:test
+- org.springframework.boot:spring-boot-test-autoconfigure:jar:3.3.1:test
+- com.jayway.jsonpath:json-path:jar:2.9.0:test
| \- org.slf4j:slf4j-api:jar:2.0.13:compile
+- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.2:compile
+- net.minidev:json-smart:jar:2.5.1:test
| \- net.minidev:accessors-smart:jar:2.5.1:test
| \- org.ow2.asm:asm:jar:9.6:test
+- org.assertj:assertj-core:jar:3.25.3:test
| \- net.bytebuddy:byte-buddy:jar:1.14.17:test
+- org.awaitility:awaitility:jar:4.2.1:test
+- org.hamcrest:hamcrest:jar:2.2:test
+- org.junit.jupiter:junit-jupiter:jar:5.10.2:test
| +- org.junit.jupiter:junit-jupiter-api:jar:5.10.2:test
| | +- org.opentest4j:opentest4j:jar:1.3.0:test
| | +- org.junit.platform:junit-platform-commons:jar:1.10.2:test
| | \- org.apiguardian:apiguardian-api:jar:1.1.2:test
| +- org.junit.jupiter:junit-jupiter-params:jar:5.10.2:test
| \- org.junit.jupiter:junit-jupiter-engine:jar:5.10.2:test
| \- org.junit.platform:junit-platform-engine:jar:1.10.2:test
+- org.mockito:mockito-core:jar:5.11.0:test
| +- net.bytebuddy:byte-buddy-agent:jar:1.14.17:test
| \- org.objenesis:objenesis:jar:3.3:test
+- org.mockito:mockito-junit-jupiter:jar:5.11.0:test
+- org.skyscreamer:jsonassert:jar:1.5.1:test
| \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
+- org.springframework:spring-core:jar:6.1.10:compile
| \- org.springframework:spring-jcl:jar:6.1.10:compile
+- org.springframework:spring-test:jar:6.1.10:test
\- org.xmlunit:xmlunit-core:jar:2.9.1:test
搜索关键字 - 1 :
jaxb
| \- org.glassfish.jaxb:jaxb-runtime:jar:4.0.5:runtime
| \- org.glassfish.jaxb:jaxb-core:jar:4.0.5:runtime
| +- org.glassfish.jaxb:txw2:jar:4.0.5:runtime
搜索关键字 - 2 :
xml
+- jakarta.xml.bind:jakarta.xml.bind-api:jar:4.0.2:compile
搜索关键字 - 3 :
activation
| | +- jakarta.activation:jakarta.activation-api:jar:2.1.3:compile
| | \- org.eclipse.angus:angus-activation:jar:2.0.2:runtime
请注意,pom.xml 仅添加以下 Spring Boot 启动器:
spring-boot-starter-jersey
spring-boot-starter-web
spring-boot-starter-web-services
(1)在使用Spring Boot时,请善用Spring Boot Starter,并添加相应的starter。不需要一一设置依赖包和版本。
(2)当使用java 17时,对应的Spring Boot版本应为Spring Boo 3.x。不是 Spring Boot 2.x.
如果想手动一一添加依赖的jar和版本设置,可以参考上面
mvn dependency:tree
的执行结果一一添加需要的jar和版本。