使用 Java 8 Intellij 项目进行 Spring 启动

问题描述 投票:0回答:1

我想用 java 8 inn Intellij 创建一个 Springoot 项目,但我不能: 当我尝试创建标准方式时,我只找到 Java 17 和 21 可供选择: enter image description here

之后我使用 Java 21 创建了一个普通项目,然后尝试缩小 java 版本。我重新配置了 pom.xml 并收到此错误:

C:\..\test\src\main\java\com\example\test\HelloController.java:4:47 java: cannot access org.springframework.web.bind.annotation.GetMapping bad class file: /C:/Users/../.m2/repository/org/springframework/spring-web/6.1.3/spring-web-6.1.3.jar!/org/springframework/web/bind/annotation/GetMapping.class class file has wrong version 61.0, should be 52.0 Please remove or make sure it appears in the correct subdirectory of the classpath.

这是我的pom.xml

<?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.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <description>test</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>


<!--    <properties>-->
<!--        <java.version>1.8</java.version>-->
<!--        <maven.compiler.source>1.8</maven.compiler.source>-->
<!--        <maven.compiler.target>1.8</maven.compiler.target>-->
<!--    </properties>-->
    <dependencies>
        <!-- Spring Boot Starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.6.4</version> <!-- Replace with the latest version -->
        </dependency>

        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.4</version> <!-- Replace with the latest version -->
        </dependency>

        <!-- Spring Boot Starter Data JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.6.4</version> <!-- Replace with the latest version -->
        </dependency>

        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version> <!-- Replace with the latest version compatible with your MySQL version -->
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version> <!-- Replace with the latest version -->
            <scope>provided</scope>
        </dependency>

        <!-- Spring Boot DevTools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>2.6.4</version> <!-- Replace with the latest version -->
            <scope>runtime</scope>
        </dependency>

        <!-- Spring Boot Starter Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.6.4</version> <!-- Replace with the latest version -->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
            <version>1.4.199</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version> <!-- Use the appropriate version -->
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ps: 我有Java 8 并安装了 Maven 3.8.5。

我尝试将java版本缩小到1.8并添加maven编译器1.8

有什么想法吗?谢谢你

java spring-boot intellij-idea java-8 spring-boot-maven-plugin
1个回答
0
投票

您收到的错误告诉您必须更改的所有内容:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.2</version> // downgrade the version here || as example 2.7.8
    <relativePath/> <!-- lookup parent from repository -->
</parent>

因为它是您的依赖项的父项,并且它控制您正在使用的依赖项的版本。当前版本

3.2.2
不支持
jdk 8
jdk
所需的最低版本是
17

还有与

spring-boot
无关的其他依赖项,您必须自行检查是否与
jdk 8
兼容。

© www.soinside.com 2019 - 2024. All rights reserved.