我有一个在
jdk 8
和 Liberty 22.0.0.10
上运行良好的应用程序。我正在将我的应用程序升级到 jdk 21
上的 Liberty 24.0.0.9
,并更改了 pom.xml
和服务器中的一些功能。应用程序启动,REST 端点和 jms 队列工作,但是当调用数据库时,它会得到一个空指针,因为实体管理器 em
没有注入(对于 jdk8,它被正确注入并且不为空)。出现此错误:
nliberty.opentracing.internal.OpentracingJaxRsEMCallbackImpl W CWMOT0009W: The OpenTracing exception mapper detected and is handling an unhandled exception from the JAX-RS application.
java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager.find(java.lang.Class, Object)" because "this.em" is null
我正在使用jpa-2.2库,从here看来jdk 21是受支持的。这些是我的pom中最相关的部分(我从jdk8版本升级了一些库):
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.extension</artifactId>
<version>4.0.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.22.2</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.openliberty.features</groupId>
<artifactId>microProfile-4.0</artifactId>
<version>24.0.0.9</version>
<type>esa</type>
<exclusions>
<exclusion>
<groupId>io.openliberty.features</groupId>
<artifactId>com.ibm.websphere.appserver.javax.jsf-2.2</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.openliberty.features</groupId>
<artifactId>jpa-2.2</artifactId>
<version>24.0.0.9</version>
<type>esa</type>
</dependency>
<dependency>
<groupId>io.openliberty.features</groupId>
<artifactId>beanValidation-2.0</artifactId>
<version>24.0.0.9</version>
<type>esa</type>
</dependency>
<dependency>
<groupId>io.openliberty.features</groupId>
<artifactId>mpContextPropagation-1.0</artifactId>
<version>24.0.0.9</version>
<type>esa</type>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>3.3.3</version>
<configuration>
<fork>true</fork>
<compilerArguments>--add-modules=javax.annotation</compilerArguments>
</configuration>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<processors> <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
</processor>
</processors>
<outputDirectory>${build.java.generated}</outputDirectory>
<compilerArguments>
-Aeclipselink.persistencexml=${project.basedir}/src/local/resources/META-INF/persistence.xml
</compilerArguments>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>process-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${build.java.generated}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这是我在 liberty 服务器上的功能(我从 jdk8 版本升级了一些库):
<server>
<featureManager>
<feature>localConnector-1.0</feature>
<feature>appSecurity-3.0</feature>
<feature>microProfile-3.3</feature>
<feature>jpa-2.2</feature>
<feature>beanValidation-2.0</feature>
<feature>mpContextPropagation-1.0</feature>
<feature>jndi-1.0</feature>
<feature>cdi-2.0</feature>
<feature>passwordUtilities-1.0</feature>
<feature>transportSecurity-1.0</feature>
<feature>ssl-1.0</feature>
<feature>ejbLite-3.2</feature>
<feature>concurrent-1.0</feature>
<feature>ejbPersistentTimer-3.2</feature>
</featureManager>
</server>
我尝试使用 jakarta 而不是 javax 但没有成功(它也破坏了我的 jms 队列),知道如何修复这个空指针吗?
<compilerArguments>--add-modules=javax.annotation</compilerArguments>
对我来说看起来有点可疑。 我希望使用依赖项 javax.annotation:javax.annotation-api:1.3
来代替。 您的某些依赖项没有 provided
的范围,它们是由自由提供的,因此这也可能是需要考虑的事情。
提供一些有关未注入 EntityManager 的代码的详细信息将有所帮助。 除了您提供的警告/错误之外,messages.log 中是否还有其他警告/错误。 从 Java 11 开始,一些 Java EE API 已从 JDK 中删除,需要由 liberty 功能提供,而以前不需要。 您的代码可能需要其中之一,但根据您上面提供的内容,我猜不需要,但我可能是错的。