步骤1:创建 maven 工程
创建一个空 Maven 工程,如下:
步骤2:pom.xml 文件中添加 SpringBoot 依赖
需要添加 SpringBoot 项目父级依赖和 SpringBoot 项目相关依赖,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>com.binge</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--Spring Boot 项目的父级依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- springboot 依赖-->
<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>
<!-- web 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok 依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
</dependencies>
</project>
步骤3:创建 SpringBoot 项目启动类
创建 com.binge.SpringBootDemoApplication.java 启动类,如下:
package com.binge;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
运行启动类,如下:
2024-07-10 11:10:31.812 INFO 21140 --- [ main] com.binge.SpringBootDemoApplication : Starting SpringBootDemoApplication using Java 1.8.0_202 on Binge2022 with PID 21140 (C:\Users\Administrator\Desktop\worksapce\SpringBootDemo\target\classes started by Binge in C:\Users\Administrator\Desktop\worksapce\SpringBootDemo)
2024-07-10 11:10:31.816 INFO 21140 --- [ main] com.binge.SpringBootDemoApplication : No active profile set, falling back to 1 default profile: "default"
2024-07-10 11:10:32.483 INFO 21140 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2024-07-10 11:10:32.489 INFO 21140 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-07-10 11:10:32.490 INFO 21140 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.79]
2024-07-10 11:10:32.584 INFO 21140 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-07-10 11:10:32.585 INFO 21140 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 739 ms
2024-07-10 11:10:32.838 INFO 21140 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2024-07-10 11:10:32.845 INFO 21140 --- [ main] com.binge.SpringBootDemoApplication : Started SpringBootDemoApplication in 1.272 seconds (JVM running for 2.059)
通过以上 SpringBoot 的启动信息可以看到,SpringBoot 内置的 Tomcat 服务器已经启动,默认监听端口为 8080
步骤4:创建 SpringBoot 全局配置文件
在工程 resources 文件下新建 application.properties 全局配置文件,文件内容如下:
#设置服务器监听端口
server.port=8080
步骤5:测试 SpringBoot 处理 Web 请求
1. 创建 User 数据模型
package com.binge.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String username;
private String password;
private String email;
private String phone;
}
2. 创建 UserController 页面处理器
package com.binge.controller;
import com.binge.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class UserController {
// 处理用户注册请求
@PostMapping("/user/register")
public User register(@RequestBody User user) {
log.info("获取用户信息:{}", user);
return user;
}
}
- @RestController 注解:处理客户端使用 AJAX 方式发送的 Web 请求
- @PostMapping("/user/register") 注解: 将具体处理 Web 请求的方法和客户端发送的 Web 请求的路径之间建议映射关系
- @RequestBody:将 Web 请求携带的 JSON 格式数据转换为 Java 对象
- @Slf4j 注解:支持使用 log4j2 框架打印日志
3. 使用 postman 工具模拟向服务器发送用户注册请求
- 请求路径:localhost:8080/user/register
- 请求方式:POST
测试成功~
标签:10,SpringBoot,import,org,main,21140,入门 From: https://www.cnblogs.com/binbingg/p/18293681