@ExceptionHandler自定义异常

文章介绍了如何在SpringMVC中使用@ExceptionHandler注解来实现自定义的异常处理。通过创建一个全局异常处理类GlobalExceptionHandler,使用@ControllerAdvice和@ExceptionHandler结合,可以捕获并处理所有Controller中的异常,返回自定义的错误信息。

@ExceptionHandler注解我们一般是用来自定义异常的。
可以认为它是一个异常拦截器(处理器)。

控制层TestControll调用

//方法一
@Controller
@RequestMapping("/admin")
public class TestControll extends GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(TestControll.class);

    @Autowired
    private UserInfoSerimpl userInfoSerimpl;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        logger.info("11111111111");
        userInfoSerimpl.saveUserInfo();

        logger.info("2222222222");
        return "sdfsfs";
    }
}

public class GlobalExceptionHandler {

    private final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({Exception.class})    //申明捕获那个异常类
    @ResponseBody
    public String handleAndReturnData(Exception e) {
        logger.error(e.getMessage(), e);
        return "自定义异常返回";
    }

}
//方法二
@Controller
@RequestMapping("/admin")
public class TestControll {
    private Logger logger = LoggerFactory.getLogger(TestControll.class);

    @Autowired
    private UserInfoSerimpl userInfoSerimpl;

    @ResponseBody
    @RequestMapping("/test")
    public String test(){
        logger.info("11111111111");
        userInfoSerimpl.saveUserInfo();

        logger.info("2222222222");
        return "sdfsfs";
    }
}


/**
 * @ControllerAdvice: 表示当前类是异常处理类,给Controller控制器类增强功能的
 *               位置: 在类的上面
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private final Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({Exception.class})    //申明捕获那个异常类
    @ResponseBody
    public String handleAndReturnData(Exception e) {
        logger.error(e.getMessage(), e);
        return "自定义异常返回";
    }

}
@ControllerAdvice 是一个注解,用于定义全局异常处理器类。我们可以在该类中定义多个 @ExceptionHandler 注解方法来处理不同类型的异常。当系统中出现异常时,会根据异常类型找到对应的 @ExceptionHandler 方法进行处理。 具体的步骤如下: 1. 创建一个类,并用 @ControllerAdvice 注解标记。 2. 在该类中定义多个方法,每个方法上使用 @ExceptionHandler 注解,并传入对应的异常类型作为参数。 3. 在方法中编写具体的异常处理逻辑。 例如,我们可以创建一个全局异常处理器类 GlobalExceptionHandler: ```java @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { // 处理 Exception 类型的异常 // 返回自定义的错误信息 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error"); } @ExceptionHandler(RuntimeException.class) public ResponseEntity<String> handleRuntimeException(RuntimeException e) { // 处理 RuntimeException 类型的异常 // 返回自定义的错误信息 return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Bad Request"); } // 可以添加更多的异常处理方法... } ``` 在上述例子中,我们定义了两个异常处理方法,分别处理 Exception 类型和 RuntimeException 类型的异常。当系统中抛出对应的异常时,会调用相应的处理方法并返回自定义的错误信息。 这样,在整个系统中任何地方出现的对应类型的异常都会被统一处理,提高了代码的可维护性和错误处理的一致性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值