全局异常处理类

全局异常处理类

全局异常处理是编程中的一个良好的时间,其目的就在于提高程序的稳定性、可靠性以及用户的体验感

全局异常处理类的需要两个关键的注解
 @RestControllerAdvice  标志这个类是全局异常处理类 所有异常都会抛到这里
 @ExceptionHandler  指定可以捕获哪种类型的异常进行处理 可以自定义异常

案例:

在这里插入图片描述

实际解决方案

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(BizIllegalException.class)  //自定义异常 业务异常
    public ResponseEntity<String> exceptionHandler(BizIllegalException e){
        log.error("controller call error",e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = DuplicateKeyException.class)
    public ResponseEntity<String> exceptionHandler(DuplicateKeyException e){
        log.error("controller call error",e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("已存在此名称的对象");
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> exceptionHandler(Exception e){
        log.error("error",e);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
    }
}

方案写的比较简单可以根据自己的需求进行添加和修改

自定义异常

/**
 * 逻辑异常
 */
public class BizIllegalException extends RuntimeException{
    public BizIllegalException (String errorMsg){
        super(errorMsg);
    }
}
在Spring Boot中,可以通过编写一个全局异常处理来捕获并处理应用程序中的异常。以下是一个示例: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleException(Exception ex) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage("Internal Server Error"); errorResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } @ExceptionHandler(NotFoundException.class) public ResponseEntity<ErrorResponse> handleNotFoundException(NotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse(); errorResponse.setMessage(ex.getMessage()); errorResponse.setStatus(HttpStatus.NOT_FOUND.value()); return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND); } // 添加其他自定义异常的处理方法 } ``` 在上面的示例中,`@ControllerAdvice`注解用于声明这是一个全局异常处理。`@ExceptionHandler`注解用于指定处理特定异常型的方法。 在`handleException`方法中,我们处理了 `Exception` 型的异常,并返回一个包含错误信息和状态码的 `ErrorResponse` 对象。 在`handleNotFoundException`方法中,我们处理了自定义的 `NotFoundException` 型的异常,并返回一个包含错误信息和状态码的 `ErrorResponse` 对象。 你可以根据自己的需求添加其他的异常处理方法。确保在每个处理方法中返回合适的 `ErrorResponse` 对象。 注意:为了让Spring Boot自动扫描到该全局异常处理,需要将它放在主应用程序所在的包或子包中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值