gpt4 book ai didi

java - spring boot 无法在 java 11 中加载和执行测试

转载 作者:行者123 更新时间:2023-11-30 05:34:45 30 4
gpt4 key购买 nike

使用 java 11 执行 Spring Boot 测试时得到以下输出。
当我执行时,我也遇到了完全相同的错误

java -jar target\application.jar

所以这不仅仅是一个 Maven 问题。

***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist.
The attempt was made from the following location:

org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.<init>(CommonAnnotationBeanPostProcessor.java:627)
javax.annotation.Resource.lookup()Ljava/lang/String; but it does not exist. Its class, javax.annotation.Resource, is available from the following locations:


jar:file:/C:/Users/X/.m2/repository/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar!/javax/annotation/Resource.class
jar:file:/C:/Users/X/.m2/repository/javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar!/javax/annotation/Resource.class

It was loaded from the following location:

file:/C:/Users/X/.m2/repository/javax/annotation/jsr250-api/1.0/jsr250-api-1.0.jar

似乎加载了错误的依赖项?我在 pom.xml

中有这些配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<dependencies>
...
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>

带有构建配置...

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <!-- or newer version -->
<configuration>
<source>11</source> <!-- depending on your project -->
<target>11</target> <!-- depending on your project -->
<compilerArgs>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>

解决此问题的正确方法是什么,以便测试可以立即正确执行?

行动:

更正应用程序的类路径,使其包含单个兼容版本的 javax.annotation.Resource

编辑

我也尝试过:

<build>
<extensions>
<extension>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</extension>
<extension>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

最佳答案

如果你愿意的话,你可以尝试这样的事情;

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>

我正在使用这种声明来添加 javax.activation-apiplexus-archiverdockerfile-maven-plugin插入。但我的spring-boot-starter-parent版本是2.1.4.RELEASEmaven-compiler-plugin版本是3.8.0 .

关于java - spring boot 无法在 java 11 中加载和执行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56865918/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com