美文网首页
全局异常处理

全局异常处理

作者: 紫菜_5eac | 来源:发表于2021-04-11 23:12 被阅读0次

@Slf4j

@RestControllerAdvice

public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(value = RuntimeException.class)

    public Result hadler(RuntimeException e){

        log.error("运行时异常:-------------{}",e);

    return Result.fail(e.getMessage());

    }

//    @ResponseStatus(HttpStatus.UNAUTHORIZED)

//    @ExceptionHandler(value = ShiroException.class)

//    public Result hadler(ShiroException e){

//        log.error("运行时异常:-------------{}",e);

//        return Result.fail(401,e.getMessage(),null);

//    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(value = MethodArgumentNotValidException.class)

    public Result hadler(MethodArgumentNotValidException e){

        log.error("实体校验异常:-------------{}",e);

        BindingResult bindingResult = e.getBindingResult();

        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();

        return Result.fail(objectError.getDefaultMessage());

    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)

    @ExceptionHandler(value = IllegalArgumentException.class)

    public Result hadler(IllegalArgumentException e){

        log.error("Assert异常:-------------{}",e);

        return Result.fail(e.getMessage());

    }

}

相关文章

网友评论

      本文标题:全局异常处理

      本文链接:https://www.haomeiwen.com/subject/uottlltx.html