Java SpringBoot的ProblemDetail实现全局异常统一处理让接口不在需要catch/ProblemDetail实现错误处理的标准化

在开发 Web 应用时,有效的错误处理和响应是提升用户体验和系统健壮性的关键。Spring Boot 3.2 引入了对 ProblemDetail 的更好支持,使得错误处理更加标准化和便捷。本文将通过实战演示,带你深入了解如何在 Spring Boot 3.2 中使用 ProblemDetail 实现错误处理的标准化。

一、ProblemDetail 简介

ProblemDetail 是 RFC 7807 定义的一种标准格式,用于在 HTTP API 中表示错误信息。它以 JSON 格式返回错误的详细信息,包括状态码、错误标题、详细描述等。Spring Boot 3.2 提供了对 ProblemDetail 的内置支持,使得我们可以轻松地在应用中实现标准化的错误响应。

二、引入依赖

在 Spring Boot 3.2 中,ProblemDetail 的支持已经内置,无需额外引入依赖。如果你使用的是 Spring Boot 3.2 以下版本,建议升级到最新版本以获得更好的支持。

三、创建自定义异常

在实际应用中,我们通常会定义一些自定义异常来表示特定的错误情况。以下是一个示例:

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

public class CustomException extends ResponseStatusException {
    public CustomException(HttpStatus status, String reason) {
        super(status, reason);
    }
}

四、全局异常处理

//本人正在项目中正在用的代码
@ControllerAdvice
@Slf4j
public class ExceptionCatchController {

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public Result exception(HttpServletRequest request, Exception ex) {
        log.error("ExceptionHandler error,url:{},meg:{}", request.getRequestURI(), ex.getMessage(), ex);
        Result result = Result.getInternalErrorResult(request.getRequestURI()
                + " 网页异常: " + ex.getMessage());
        if (ex instanceof HttpRequestMethodNotSupportedException) {
            result.setCode(405);
        }
        if (ex instanceof HttpMessageNotReadableException) {
            result.setCode(400);
        }
        if (ex instanceof PermissionsException) {
            result.setCode(403);
        }
        if (ex instanceof BaseException) {
            result.setCode(500);
            result.setMessage(ex.getMessage());
        }
        return result;
    }
}

下面是demo: 使用 @ControllerAdvice 注解创建一个全局异常处理器,可以捕获并处理各种异常。

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.server.ResponseStatusException;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ResponseStatusException.class)
    public ResponseEntity<ProblemDetail> handleResponseStatusException(ResponseStatusException ex) {
        ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(ex.getStatus(), ex.getReason());
        return ResponseEntity.status(ex.getStatus()).body(problemDetail);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ProblemDetail> handleException(Exception ex) {
        ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(problemDetail);
    }
}

五、使用 ProblemDetail 返回错误信息

在控制器中,我们可以直接返回 ProblemDetail 对象,Spring Boot 会自动将其转换为标准的 JSON 格式。

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

@RestController
public class TestController {

    @GetMapping("/test")
    public ResponseEntity<ProblemDetail> test() {
        ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, "This is a test error");
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail);
    }
}

六、测试与验证

启动应用后,访问 /test 接口,可以看到返回的错误信息符合 ProblemDetail 的标准格式:

{
  "type": "https://httpstatuses.org/400",
  "title": "Bad Request",
  "status": 400,
  "detail": "This is a test error"
}

七、总结

通过使用SpringBoot3.2的ProblemDetail,我们可以轻松实现错误处理的标准化,提升应用的用户体验和系统健壮性。希望本文的内容对你有所帮助,让你在实际项目中能够更好地处理错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java皇帝

有帮助就赏点吧,博主点杯水喝喝

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值