我正在使用 Java 11 的 Maven 和 IntelliJ IDEA 上的 Maven。我正在尝试创建一个 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 {...}
}
但构建在以下导入上失败:
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;
它失败并出现错误:
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.
我知道这个问题与 Java 版本有关,但找不到任何有效的解决方案。
使用在 JAVA_HOME 中设置的 Java 11。
我尝试过的:
- 将 JDK 更新到 Java 11。
-
java -version
gives: openjdk version "11.0.17" 2022-10-18 LTS
- 更新 IntelliJ 中的项目以使用 Java 11。
- Remove
.idea
folder.
- 使缓存无效
有人知道如何修复令人困惑的版本吗?
您使用的是 Spring Framework 6,从 Spring 6 开始,支持的最低 Java 版本是 Java 17(类版本 61 是 Java 17 的类版本)。
正如记录在Spring框架概述 https://docs.spring.io/spring-framework/docs/current/reference/html/overview.html#overview春季 6:
从 Spring Framework 6.0 开始,Spring 需要 Java 17+。
所以,如果你想使用Spring 6,你需要升级到Java 17。如果你想继续使用Java 11,你需要降级到Spring 5.3。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)