Spring Boot错误处理

Spring Boot常见的错误处理方法有三种,在我们工作中,采取一种即可。

如果我们不处理错误,Spring Boot默认给我们的错误页面如下:
这里写图片描述

方法一


  Spring Boot默认将所有的错误默认映射到/error,实现ErrorController

1.首先我们写一个BaseErrorController

    package com.example.mx.controller;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.web.servlet.error.ErrorController;
    import org.springframework.stereotype.Controller;

    @Controller
    @RequestMapping(value = "error")
    public class BaseErrorController implements ErrorController {

        private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);

        @Override
        public String getErrorPath() {
            logger.error("出错了,进入默认的错误控制器!!!");
            return "error/error";
        }

        @RequestMapping
        public String error() {
            return getErrorPath();
        }
    }

2.我们在/templates/下创建error文件夹,并创建一个error.ftl的错误模版
这里写图片描述
模版代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>Spring Boot ErrorPage</title>
</head>
<body>
    <p><center>系统出错,请联系管理员!</center></p>
</body>
</html>

3.启动项目,我们访问一个未定义的地址
这里写图片描述
  说明我们自定义的错误页面生效了。

方法二


  添加自定义的错误页面:

  html静态页面:resources/public/error/下定义,例如添加404页面resources/public/error/404.html
  模版引擎页面:templates/error/下定义,例如添加5xx页面templates/error/5xx.ftl
  注意:templates/error/优先级高于resources/public/error/

1.创建404.html文件
这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Spring Boot 404 Error</title>
</head>
<body>
    <p><center>页面可能被外星人偷走了......</center></p>
</body>
</html>

2.启动项目,我们访问一个未定义的地址
这里写图片描述
  说明配置404错误页面已生效。


1.创建5xx.ftl文件
这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <title>Spring Boot 5xx Error</title>
</head>
<body>
    <p>系统5xx异常出错,请联系管理员!</p>
</body>
</html>

2.定义一个错误代码

package com.example.mx.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/web")
public class WebController {

    private static Logger logger = LoggerFactory.getLogger(WebController.class);

    @RequestMapping("index")
    public String index(ModelMap map) {
        logger.info("这是controller!!!!!!!!");
        map.put("title", "hello world");
        return "index";
    }

    @RequestMapping("errorTest")
    public String errorTest(ModelMap map) {
        logger.error("这是errorTest!!!!!!!!");
        throw new RuntimeException("未知的系统错误!!!");
    }
}

3.启动项目,5xx错误地址
这里写图片描述

方法三


  使用@ControllerAdvice注解

1.自定义一个全局异常处理的类,在这个类里我们可以针对各种异常写不同的处理,跳转到不同的错误页面,我们先创建一个ErrorHandle类,试着处理RuntimeException
这里写图片描述

package com.example.mx.errorHandle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ErrorHandle {

    private static final Logger logger = LoggerFactory.getLogger(ErrorHandle.class);

    /**
     * 运行时异常处理
     * @param exception
     * @return
     */
    @ExceptionHandler({ RuntimeException.class })
    public ModelAndView exception(RuntimeException exception) {

        logger.info("自定义异常处理-RuntimeException");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", exception.getMessage());
        modelAndView.setViewName("500");
        return modelAndView;
    }
}

2.在templates/下创建500.ftl错误页面,如上上张图所示

<!DOCTYPE html>
<html>
<head>
    <title>Spring Boot 500 Error</title>
</head>
<body>
    <p>系统500异常出错,请联系管理员!</p>
    <h1>${message}</h1>
</body>
</html>

3.我们接着访问开始创建的WebController中的errorTest方法,500错误页面
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值