Spring Boot 2.6.3 requires Java 8 and is compatible up to and including Java 17. Spring Framework 5.3.15 or above is also required.
Spring Boot 2.6.3 需要Java 8,并且兼容 Java 17(包括 Java 17 )。还需要Spring Framework 5.3.15或更高版本。
1、创建maven项目
maven设置本地仓库及阿里云仓库镜像,设置编译jdk版本,减少不必要的错误。
<!-- 阿里云仓库 -->
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
<!-- 告诉 Maven 用 jdk1.8 来编译和运行项目 -->
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
2、添加依赖
给项目添加父项目依赖及web依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、编写启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author zhangxuhui
* @Date 2022/2/14
* @email zxh_1633@163.com
*/
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class,args);
}
}
4、编写主配置文件
以application.properties命名的配置文件
#端口号
server.port=9001
5、控制器
/**
* @Author zhangxuhui
* @Date 2022/2/14
* @email zxh_1633@163.com
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello SpringBoot 2.6.3";
}
}
6、验证结果
在编辑器中直接启动
7、打成jar可执行文件
添加maven构建依赖
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
构建jar包:1、进入到项目路径下,运行mvn package打包;2、在编辑器中打包
8、运行可执行文件
打包完毕后可以在项目target目录下找到jar包,在同级目录下执行java -jar命令运行。
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.3)
....... . . .
....... . . . (log output here)
....... . . .
........ Started MyApplication in 2.536 seconds (JVM running for 2.864)