Spring5升级内容


1.创建一个Maven项目


2.配置项目文件pom.xml
如果出现红色报错,先重载一下Maven,然后重启Idea
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.atgg</groupId>
<artifactId>boot-01-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.创建一个MainApplication.class文件
package com.atgg.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class,args);
}
}
4.创建一个控制器controller,处理响应请求

HelloController.class
package com.atgg.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//请求映射,当发出/hello的请求时,回复一个字符串:Hello, Spring Boot 2!
@RequestMapping("/hello")
public String handle01(){
return "Hello, Spring Boot 2!";
}
}
4.点击MainApplication.class的main函数的run开始运行程序:

在浏览器输入本地请求:
http://localhost:8080/hello
页面会显示字符串:Hello, Spring Boot 2!

5.简化配置
在resources包中新建一个文件:application.properties

修改请求的端口号为8888
server.port=8888
相应的,请求页面为8888时才会显示对应的字符串:

6.打包成可执行文件(.jar)
在pom.xml中添加以下字段:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.4.RELEASE</version>
</plugin>
</plugins>
</build>

点击Maven,将其打成jar包

进入target文件夹就可以找到.jar包了,进入cmd
输入命令执行:
java -jar boot-01-helloworld-1.0-SNAPSHOT.jar(jar包名)
照样可以执行响应请求

注意点:
取消掉cmd的快速编辑模式
本文介绍如何使用SpringBoot创建一个简单的Web应用,包括搭建Maven项目、配置依赖、编写启动类和控制器等内容,并演示了如何通过浏览器访问应用。


被折叠的 条评论
为什么被折叠?



