Banner

Spring Boot 快速上手教程

2026-10-13

Spring Boot 快速上手建议直接选 Spring Boot 4.1 + JDK 21,按“环境 → 建项目 → 写接口 → 启动测试”这条主线,10 分钟就能跑通第一个 REST API。

1. 准备环境

需要:

  • JDK 21 或以上
  • Maven 3.9+ 或 Gradle 8+
  • IntelliJ IDEA 社区版/终极版
  • 接口测试工具:浏览器、Postman、Apifox 任选其一

验证 JDK:

java -version

2. 用 Spring Initializr 建项目

打开:https://start.spring.io/

推荐配置:

配置项推荐值
ProjectMaven
LanguageJava
Spring Boot4.1.x
Groupcom.example
Artifactdemo
PackagingJar
Java21
DependenciesSpring Web

点击 Generate,下载 ZIP,解压后用 IDEA 打开。

3. 写第一个接口

主启动类通常已经生成好了,例如:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

新建控制器 HelloController.java:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot 4.1!";
    }
}

4. 启动项目

IDEA 中直接运行 DemoApplication,或在项目根目录执行:

./mvnw spring-boot:run

看到类似输出就说明启动成功:

Tomcat started on port 8080

浏览器访问:

http://localhost:8080/hello

页面返回:

Hello, Spring Boot 4.1!

5. 写一个简单 CRUD 接口

先建一个简单对象:

package com.example.demo.model;

public class User {
    private Long id;
    private String name;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

再写控制器:

package com.example.demo.controller;

import com.example.demo.model.User;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/users")
public class UserController {

    private final Map<Long, User> users = new ConcurrentHashMap<>();

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return users.get(id);
    }

    @PostMapping
    public String createUser(@RequestBody User user) {
        users.put(user.getId(), user);
        return "created";
    }

    @PutMapping("/{id}")
    public String updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        users.put(id, user);
        return "updated";
    }

    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "deleted";
    }
}

用 Postman 或 Apifox 测试:

  • GET http://localhost:8080/users/1
  • POST http://localhost:8080/users,Body 选 JSON:
{
  "id": 1,
  "name": "Spring Boot"
}

6. 开启虚拟线程

Spring Boot 4.x 已经深度适配虚拟线程,在 application.yml 里加一行即可:

spring:
  threads:
    virtual:
      enabled: true

这个配置适合高并发、IO 密集型接口,比如查询、调用第三方 API、消息处理等场景。

7. 下一步可以往哪走

  • 接数据库:加 spring-boot-starter-data-jpa 或 MyBatis-Plus,把内存 Map 换成真实数据库。
  • 接前端:后端提供 REST API,前端用 Vue/React 调用。
  • 接监控:加 spring-boot-starter-actuator,查看健康检查和指标。
  • 接 AI/微服务:4.1 开始官方支持 gRPC 和 OpenTelemetry,适合往后端工程化方向继续学。
6