springboot3时代创建springboot2程序项目
目前,IDEA,springboot 官网,都不能创建基于JDK8(JDK1.8)的springboot程序了。
解决办法:
1. 先在官网(https://start.spring.io/)初始化一个 springboot3程序 (JDK17)。
2.解压压缩包,并修改pom.xml 文件 。
2.1 找到spring-boot-starter-parent,把 <version>3.1.9</version>修改为 <version>2.7.0</version>
2.2 找到 java.version,修改为 <java.version>1.8</java.version>。
3. 如果 String 报红字错码,鼠标移动上去,会提示你指定 JDK,选择JDK8 即可。
完整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>2.7.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>apidemo1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>apidemo1</name> <description>api port test</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <image> <builder>paketobuildpacks/builder-jammy-base:latest</builder> </image> </configuration> </plugin> </plugins> </build> </project>
Apidemo1Application:
package com.example.apidemo1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Apidemo1Application { public static void main(String[] args) { SpringApplication.run(Apidemo1Application.class, args); } }
HomeController:
package com.example.apidemo1; import org.springframework.boot.system.ApplicationHome; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.io.File; @RestController public class HomeController { @RequestMapping("/test") String index() { String msg = "123"; try { System.out.println( "msg:" + msg); } catch (Exception ex) { msg = ex.getMessage(); } return "Hello Spring Boot " + msg; } public String getJarRoot() { ApplicationHome home = new ApplicationHome(getClass()); File jarFile = home.getSource(); return jarFile.getParentFile().getPath(); } }
。。