我正在使用Maven with Java 11 和 Maven on IntelliJ IDEA。我正在尝试创建一个 JsonTset 类,如下所示:
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.nio.file.Paths;
@JsonTest
public class JacksonInjectExample {
private static final String filePath = "test.json";
private static final String JSON = String.format(String.valueOf(Paths.get(filePath)));
@Autowired
private JacksonTester<OHStepTrackingNotification> jacksonTester;
@Configuration
public static class TestConfiguration {...}
@BeforeClass
static public void setSystemProperties() throws IOException {...}
@Test
public void test() throws IOException {...}
}
Run Code Online (Sandbox Code Playgroud)
但构建在以下导入上失败:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Run Code Online (Sandbox Code Playgroud)
它失败并出现错误:
java: cannot access org.springframework.beans.factory.annotation.Autowired
bad class file: /C:/Users/<username>/.m2/repository/org/springframework/spring-beans/6.0.2/spring-beans-6.0.2.jar!/org/springframework/beans/factory/annotation/Autowired.class
**class file has wrong version 61.0, should be 55.0**
Please remove or make sure it appears in the correct subdirectory of the classpath.
Run Code Online (Sandbox Code Playgroud)
我知道这个问题与Java版本有关,但我找不到任何有效的解决方案。
使用在 JAVA_HOME 中设置的 Java 11。
我尝试过的:
java -version给出:openjdk version "11.0.17" 2022-10-18 LTS.idea文件夹。如何修复令人困惑的版本?
Mar*_*eel 117
您使用的是 Spring Framework 6,从 Spring 6 开始,支持的最低 Java 版本是 Java 17(类版本 61 是 Java 17 的类版本)。
从 Spring Framework 6.0 开始,Spring 需要 Java 17+。
所以,如果你想使用Spring 6,你需要升级到Java 17。如果你想继续使用Java 11,你需要降级到Spring 5.3。
鉴于 Spring Boot 3 是基于 Spring Framework 6 构建的,这同样适用于 Spring Boot 3:您需要升级到 Java 17,或者需要降级到 Spring Boot 2.7。
小智 6
可能是\xe2\x80\x99s因为Java版本和Spring或Spring Boot版本不一致。检查您的 Spring 版本。以下是 Spring 网站的一个片段:
\n\n\n\n我们计划于 2022 年 11 月发布 Spring Boot 3.0。下一个\n主要修订将基于 Spring Framework 6.0,并且需要 \nJava 17 或更高版本。它还将是第一个使用 Jakarta EE 9 API (jakarta. ) 而不是 EE 8(javax. ) 的 Spring Boot 版本。
\n
| 归档时间: |
|
| 查看次数: |
205383 次 |
| 最近记录: |