Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Spring Boot 4.0 新特性演示 本项目演示 Spring Boot 4.0.0(2025年11月20日发布)的主要新特性。 ## 环境要求 - **Java 21+**(推荐,支持虚拟线程) - **Maven 3.9+** 或 **Gradle 9** - Spring Boot 4.0.0 ## 新特性概览 ### 1. HTTP Service Clients(声明式HTTP客户端) 类似于 Feign,但是 Spring 原生支持,无需额外依赖。 ```java @HttpExchange(url = "https://api.github.com") public interface GitHubService { @GetExchange("/users/{username}") Map getUser(@PathVariable String username); } ``` **演示接口:** - `GET /github/users/{username}` - 获取GitHub用户信息 - `GET /github/repos/{owner}/{repo}` - 获取GitHub仓库信息 --- ### 2. API Versioning(API版本控制) 支持多种版本控制方式:URL路径、请求参数、请求头。 ```yaml spring: mvc: apiversion: enabled: true parameter-name: v header-name: X-API-Version default-version: 1.0 ``` **演示接口:** - `GET /api/v1/users` - V1版本(基础信息) - `GET /api/v2/users` - V2版本(详细信息) - `GET /api/v3/users` - V3版本(分页数据) - `GET /api/versions` - 查看支持的版本 --- ### 3. OpenTelemetry Starter(可观测性) 新增 `spring-boot-starter-opentelemetry`,一站式配置 OTLP 导出。 ```yaml management: otlp: tracing: endpoint: http://localhost:4318/v1/traces metrics: endpoint: http://localhost:4318/v1/metrics ``` **演示接口:** - `GET /observability/auto-trace` - 自动追踪演示 - `GET /observability/manual-observation/{operationName}` - 手动Observation - `GET /observability/nested-trace` - 嵌套追踪(模拟微服务调用链) --- ### 4. Task Decoration(多任务装饰器) 支持多个 `TaskDecorator` Bean,自动组合成 `CompositeTaskDecorator`。 ```java @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class LoggingTaskDecorator implements TaskDecorator { @Override public Runnable decorate(Runnable runnable) { return () -> { log.info("任务开始"); runnable.run(); log.info("任务结束"); }; } } ``` **演示接口:** - `POST /tasks/async/{taskName}` - 触发异步任务 - `POST /tasks/batch/{count}` - 触发批量任务 --- ### 5. RestTestClient(测试客户端) 新的测试客户端,支持流式API。 ```java @SpringBootTest @AutoConfigureMockMvc class MyTest { @Autowired private MockMvcTester mockMvc; @Test void test() { mockMvc.get().uri("/api/v1/users") .assertThat() .hasStatusOk() .bodyJson() .extractingPath("$.version") .isEqualTo("1.0"); } } ``` --- ## 运行项目 ```bash # 编译 mvn clean package # 运行 mvn spring-boot:run # 或者 java -jar target/spring-boot4-demo.jar ``` 访问 http://localhost:8090 查看所有可用接口。 --- ## 依赖升级 Spring Boot 4.0 同时升级了以下核心依赖: | 组件 | 版本 | |------|------| | Spring Framework | 7.0 | | Spring Security | 7.0 | | Spring Data | 2025.1 | | Hibernate | 7.1 | | Micrometer | 1.16 | --- ## 升级指南 从 Spring Boot 3.x 升级到 4.0: 1. 先升级到 Spring Boot 3.5 2. 确保使用 Java 21+ 3. 检查 Jakarta EE 11 兼容性 4. 参考官方 [Migration Guide](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide) --- ## 参考链接 - [Spring Boot 4.0 Release Notes](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Release-Notes) - [Spring Boot 4.0 Migration Guide](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide) - [Spring Boot 官方文档](https://docs.spring.io/spring-boot/4.0/reference/) # spring-boot-New-features

About

spring boot 4.0.0新特性样例代码

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages