You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.0.0.RELEASE迁移后,如何导入ServerRequest获取错误属性?

解决Spring Boot 2中导入ServerRequest类的问题

嘿,我懂你现在的需求——已经把项目从Spring Boot 1迁移到2.0.0.RELEASE版本,想要导入org.springframework.web.reactive.function.server.ServerRequest类,用来结合Spring Boot的错误属性接口处理相关逻辑对吧?

问题的核心在于:ServerRequest是Spring WebFlux模块的核心类,而你当前只引入了Spring Boot的父依赖,默认并没有包含WebFlux的starter包,所以IDE会找不到这个类。

解决方案:添加WebFlux Starter依赖

在你的pom.xml中加入以下依赖即可(因为父依赖已经统一管理了版本,不需要额外指定version):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

简单使用示例(结合ErrorAttributes)

假设你要通过ServerRequest配合ErrorAttributes接口获取错误属性,这里给个极简的实现示例:

import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

// 构造注入ErrorAttributes
private final ErrorAttributes errorAttributes;

public Mono<ServerResponse> handleErrorRequest(ServerRequest request) {
    // 获取请求对应的错误属性
    var errorProperties = errorAttributes.getErrorAttributes(request, true);
    // 后续可根据需求封装错误响应返回
    return ServerResponse.badRequest().bodyValue(errorProperties);
}

这样就能顺利导入ServerRequest类,完成你结合错误属性接口的开发需求啦!

内容的提问来源于stack exchange,提问作者en Peris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:00:02