spring当中异常的处理方式

1. spring当中异常的处理方式

spring当中异常的处理方式有三种:
1)全局异常处理方法
2)特定异常处理方法
3)通过自定义的方法去处理异常

1.1.全局异常处理方法

//这里的名字GlobalExceptionHandler 是自己起的
@ControllerAdvice
public class GlobalExceptionHandler {
     /**
     * 全局异常 @ExceptionHandler(Exception.class)通过异常处理器然后去获取异常
     * @param e 异常
     * @return 返回值
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail();
    }
}

1.2.特定异常处理方法

@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 全局异常 @ExceptionHandler(Exception.class)通过异常处理器然后去获取异常
     * @param e 异常
     * @return 返回值
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    /**
     *特殊异常处理 @ExceptionHandler(ArithmeticException.class) 对ArithmeticException异常的处理
     * @param e 异常
     * @return 返回值
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail().message("执行了特定异常处理");
    }
}

1.3.

1)定义异常类

package com.atguigu.common.config.exception;

import com.atguigu.common.result.ResultCodeEnum;
import lombok.Data;
//自定义的异常都需要去继承RuntimeException
@Data
public class GuiguException extends RuntimeException{
    private Integer code;
    private String message;

    /**
     * 通过状态码和错误信息创建异常信息
     * @param code 状态码
     * @param message 异常信息
     */
    public GuiguException(Integer code,String message){
        super(message);
        this.code = code;
        this.message = message;
    }

    /**
     * 通过枚举类处理异常信息
     * @param resultCodeEnum 枚举类
     */
    public GuiguException(ResultCodeEnum resultCodeEnum){
        super(resultCodeEnum.getMessage());
        this.code = resultCodeEnum.getCode();
        this.message = resultCodeEnum.getMessage();
    }

    @Override
    public String toString() {
        return "GuiguException{" +
                "code=" + code +
                ", message='" + message + '\'' +
                '}';
    }
}

2)在GlobalExceptionHandler 类里面声明我们自定义的异常

@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 全局异常 @ExceptionHandler(Exception.class)通过异常处理器然后去获取异常
     * @param e 异常
     * @return 返回值
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail();
    }

    /**
     *特殊异常处理 @ExceptionHandler(ArithmeticException.class) 对ArithmeticException异常的处理
     * @param e 异常
     * @return 返回值
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail().message("执行了特定异常处理");
    }

    /**
     * 自定义异常方式 @ExceptionHandler(GuiguException.class) 对自定义GuiguException异常的处理
     * @param e 异常
     * @return 返回值
     */
    @ExceptionHandler(GuiguException.class)
    @ResponseBody
    public Result error(GuiguException e){
        e.printStackTrace();
        return Result.fail().message(e.getMessage()).code(e.getCode());
    }
}

1.4 总结

上面说的 全局异常处理方法、特定异常处理方法、通过自定义的方法去处理异常的优先级:
通过自定义的方法去处理异常>特定异常处理方法>全局异常处理方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security提供了许多方式来处理身份验证和授权的异常。下面是一些常见的异常处理方式: 1. 使用Spring的全局异常处理器:可以通过在应用程序中定义一个`@ControllerAdvice`类,并使用`@ExceptionHandler`注解来处理Spring Security的异常。在异常处理方法中,可以根据不同的异常类型进行相应的处理,例如返回自定义的错误页面或者JSON响应。 ```java @ControllerAdvice public class SecurityExceptionHandler { @ExceptionHandler(AuthenticationException.class) public ResponseEntity<String> handleAuthenticationException(AuthenticationException ex) { // 处理身份验证异常 return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ex.getMessage()); } @ExceptionHandler(AccessDeniedException.class) public ResponseEntity<String> handleAccessDeniedException(AccessDeniedException ex) { // 处理访问拒绝异常 return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ex.getMessage()); } // 其他异常处理方法... } ``` 2. 自定义AccessDeniedHandler:可以实现`AccessDeniedHandler`接口来处理访问拒绝异常。可以在`handle()`方法中自定义处理逻辑,例如返回自定义的错误页面或者JSON响应。 ```java public class CustomAccessDeniedHandler implements AccessDeniedHandler { @Override public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) throws IOException, ServletException { // 处理访问拒绝异常 response.sendError(HttpServletResponse.SC_FORBIDDEN, ex.getMessage()); } } ``` 然后,在Spring Security配置中指定自定义的`AccessDeniedHandler`: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // ... @Override protected void configure(HttpSecurity http) throws Exception { http // ... .exceptionHandling() .accessDeniedHandler(new CustomAccessDeniedHandler()) // ... } // ... } ``` 这些只是处理Spring Security异常的两种常见方式,你可以根据实际需求选择适合的方式进行异常处理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值