spring-boot Web开发基础3-全局异常处理

一、@ControllerAdvice注解:写一个类,加上

二、@ExceptionHandler(value=Exception.class)

       创建异常处理方法,方法需要加@ExceptionHandler(value=Exception.class)注解,在方法里面处理异常

三、例如

      首先需要在指定的点抛出 RuntimeException

    catch (Exception e) {
            LOGGER.error("密文加密失败"+e.getMessage(),e);
            throw new RuntimeException("密文加密失败");
        }
 /**
     * description 拦截未知的运行时异常
     *
     * @param e 1
     * @return com.usthe.bootshiro.domain.vo.Message
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.OK)
    public Message notFoundException(RuntimeException e) {
        LOGGER.error("运行时异常:",e);
        return new Message().error(1111,"服务器开小差");
    }

猜你喜欢

转载自blog.csdn.net/lidongliangzhicai/article/details/91575676