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

Spring WebFlux自定义异常被覆盖,自定义消息无法显示

解决Spring WebFlux自定义404异常消息被覆盖的问题

我之前也碰到过一模一样的情况!这是因为Spring Boot默认的错误处理机制(WebFlux下是DefaultErrorAttributes)在构建错误响应时,默认不会把自定义异常的消息包含进去,导致你看不到自己设置的"Skeleton not found"。下面给你几个实用的解决办法:

方法1:修改Spring Boot配置(最简单)

从Spring Boot 2.3版本开始,默认不会在错误响应中包含异常消息,你只需要在配置文件里开启这个功能就行:

如果用application.properties:

server.error.include-message=always

如果用application.yml:

server:
  error:
    include-message: always

重启服务后再请求,你就能在响应的message字段看到自定义的内容了。

方法2:全局异常处理器(最灵活)

如果你想完全定制错误响应的结构,或者只针对特定异常做处理,可以用@RestControllerAdvice写一个全局异常处理器:

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SkeletonNotFoundException.class)
    public Mono<ResponseEntity<Map<String, Object>>> handleSkeletonNotFound(SkeletonNotFoundException ex, ServerWebExchange exchange) {
        Map<String, Object> errorBody = Map.of(
                "timestamp", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME),
                "path", exchange.getRequest().getPath().value(),
                "status", HttpStatus.NOT_FOUND.value(),
                "error", HttpStatus.NOT_FOUND.getReasonPhrase(),
                "message", ex.getMessage(), // 这里放入自定义异常消息
                "requestId", exchange.getRequest().getId()
        );
        return Mono.just(new ResponseEntity<>(errorBody, HttpStatus.NOT_FOUND));
    }
}

这个方法能让你完全控制返回的错误内容,甚至可以针对不同异常返回不同结构的响应。

方法3:自定义ErrorAttributes

如果你想在默认的错误响应基础上补充内容,可以继承DefaultErrorAttributes并重写方法:

import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.web.reactive.function.server.ServerRequest;
import java.util.Map;

public class CustomErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
        Map<String, Object> attributes = super.getErrorAttributes(request, includeStackTrace);
        Throwable error = getError(request);
        
        // 只针对自定义异常添加消息
        if (error instanceof SkeletonNotFoundException) {
            attributes.put("message", error.getMessage());
        }
        return attributes;
    }
}

然后需要把这个类注册为Bean:

import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ErrorConfig {
    @Bean
    public ErrorAttributes errorAttributes() {
        return new CustomErrorAttributes();
    }
}

这样默认的错误响应就会带上你自定义异常的消息了。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:30:48