You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot如何移除错误响应中的exception属性?

解决Spring Boot自定义异常返回JSON中移除exception属性的问题

我之前刚好踩过这个坑——当自定义异常加上@ResponseStatus注解后,哪怕配置了server.error.include-exception=false,Spring还是会把exception字段塞进返回的JSON里。这是因为带@ResponseStatus的异常会触发Spring特殊的错误处理逻辑,直接忽略掉这个配置。给你几个亲测有效的解决方案:

方案1:自定义ErrorAttributes(最简单直接)

Spring默认用DefaultErrorAttributes来构建错误响应内容,它对带@ResponseStatus的异常会强制添加exception属性。我们可以重写这个类,主动移除该字段:

import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {
    @Override
    public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest, options);
        // 直接移除exception字段
        errorAttributes.remove("exception");
        return errorAttributes;
    }
}

把这个类放进你的Spring组件扫描路径下,它会自动替换默认的ErrorAttributes,所有错误响应都会去掉exception属性,不管异常有没有加@ResponseStatus

方案2:用@ControllerAdvice全局控制异常响应(最灵活)

如果想完全自定义错误返回的JSON结构,直接抛弃Spring的默认错误处理,用全局异常处理器是更好的选择:

第一步:移除自定义异常上的@ResponseStatus

public class IllegalUserAgentException extends RuntimeException {
    public IllegalUserAgentException(String message) {
        super(message);
    }
}

第二步:编写全局异常处理器

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IllegalUserAgentException.class)
    public ResponseEntity<Map<String, Object>> handleIllegalUserAgent(IllegalUserAgentException ex) {
        // 获取当前请求路径
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("timestamp", LocalDateTime.now());
        errorResponse.put("status", HttpStatus.FORBIDDEN.value());
        errorResponse.put("error", HttpStatus.FORBIDDEN.getReasonPhrase());
        errorResponse.put("message", ex.getMessage());
        errorResponse.put("path", request.getRequestURI());
        
        return new ResponseEntity<>(errorResponse, HttpStatus.FORBIDDEN);
    }

    // 还可以添加其他异常的处理方法,统一管理所有错误响应
}

这种方式你可以完全控制返回的字段,想加什么、删什么都由你决定,适合需要统一错误响应格式的场景。

方案3:检查Spring Boot版本(针对旧版本)

如果你的Spring Boot版本低于2.3,可能server.error.include-exception这个配置项的行为不同,旧版本可能需要用server.error.include-exceptions=false(注意复数形式)。不过这个情况比较少见,优先试试前两个方案。

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

火山引擎 最新活动