02.8第一个Spring Boot 3应用

分类: Spring 6和Spring Boot 3基础

第一个 Spring Boot 3 应用

现在我们已经了解了 Spring Boot 3 的基础知识,让我们创建一个简单的 Hello World 应用,实践所学知识。

本节将学习:Hello World 应用、启动类编写、控制器创建,以及运行与测试。

Hello World 应用

项目结构

hello-world/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── helloworld/
│   │   │               ├── HelloWorldApplication.java
│   │   │               └── controller/
│   │   │                   └── HelloController.java
│   │   └── resources/
│   │       └── application.yml
└── pom.xml

启动类编写

HelloWorldApplication.java

package com.example.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }

说明:

  • @SpringBootApplication:标识 Spring Boot 应用
  • main 方法:应用入口
  • SpringApplication.run():启动应用

控制器创建

HelloController.java

package com.example.helloworld.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello, Spring Boot 3!"; } @GetMapping("/hello/{name}") public String helloWithName(@PathVariable String name) { return "Hello, " + name + "!"; } }

说明:

  • @RestController:标识 RESTful 控制器
  • @GetMapping:处理 GET 请求
  • @PathVariable:获取路径变量

配置文件

application.yml

server: port: 8080 spring: application: name: hello-world

运行与测试

运行应用

方式1:IDE 运行

  1. 右键点击 HelloWorldApplication.java
  2. 选择 "Run 'HelloWorldApplication'"

方式2:Maven 运行

mvn spring-boot:run

方式3:打包运行

mvn clean package java -jar target/hello-world-0.0.1-SNAPSHOT.jar

测试接口

测试命令:

# 测试 /hello curl http://localhost:8080/hello # 测试 /hello/{name} curl http://localhost:8080/hello/World

预期响应:

Hello, Spring Boot 3! Hello, World!

完整 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.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>hello-world</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hello-world</name> <description>Hello World project for Spring Boot 3</description> <properties> <java.version>21</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

官方资源

本节小结

在本节中,我们创建了:

第一个是启动类。 使用 @SpringBootApplication 标识 Spring Boot 应用。

第二个是控制器。 使用 @RestController 创建 RESTful API。

第三个是配置文件。 配置应用端口和名称。

第四个是运行和测试。 成功运行应用并测试接口。

这就是第一个 Spring Boot 3 应用。通过这个简单的例子,我们实践了 Spring Boot 的基础知识。

在下一章,我们将开始构建单体商城服务,学习如何在实际项目中使用 Spring Boot。