码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot Learning(错误处理)

时间:2017-03-12 16:39:36      阅读:960      评论:0      收藏:0      [点我收藏+]

标签:ons   throw   自定义   代码   rri   not   cto   ice   UI   

三种方式:

第一种:

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

这时候需要定义一个Controller去实现ErrorController

package com.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "error")
public class BaseErrorController implements ErrorController {
private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);

    @Override
    public String getErrorPath() {
        logger.info("出错啦!进入自定义错误控制器");
        return "error/error";
    }

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

}

同时在resource/templates下新建error目录并添加error.ftl,这样在异常发生时就会显示error.ftl页面。

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
    <h1>error-系统出错,请联系后台管理员</h1>
</body>
</html>

代码目录结构:

技术分享

 

方法二:添加自定义的错误页面 

1 html静态页面:在resources/public/error/ 下定义
如添加404页面: resources/public/error/404.html页面,中文注意页面编码


2 模板引擎页面:在templates/error/下定义
如添加5xx页面: templates/error/5xx.ftl


注:templates/error/ 这个的优先级比较 resources/public/error/高,也就是说如果templates下面如果有相应的异常处理页面会显示它的而不会显示error目录下的页面。

我们去掉刚才写的ErrorController类,同时在WebController类中制造一个异常用于测试。

package com.example.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(value = "/web")
public class WebController {

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

    @RequestMapping(value = "index")
    public String index(ModelMap map) {
        logger.info("这里是controller...");
        map.put("title", "freemarker hello word");
        return "index"; // 开头不要加上/,linux下面会出错
    }

    @RequestMapping(value = "error")
    public String error(ModelMap map) {
        throw new RuntimeException("测试异常");
    }
}

添加404.html和5xx.ftl

 

5xx.ftl: 

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
    <h1>5xx error-系统出错,请联系后台管理员</h1>
    <h1>异常信息:${exception}</h1>
</body>
</html>

 

技术分享

 

方法三:使用注解@ControllerAdvice

自定义一个异常处理类,在该类中可以使用注解拦截异常,可以针对不同异常采取不同处理方式

代码:

package com.example.handler;

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

@ControllerAdvice
public class BizExceptionHandler {

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

    /**
     * 统一异常处理
     * 
     * @param exception
     *            exception
     * @return
     */
    @ExceptionHandler({ RuntimeException.class })
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView processException(RuntimeException exception) {
        logger.info("自定义异常处理-RuntimeException");
        ModelAndView m = new ModelAndView();
        m.addObject("ericException", exception.getMessage());
        m.setViewName("error/500");
        return m;
    }

    /**
     * 统一异常处理
     * 
     * @param exception
     *            exception
     * @return
     */
    @ExceptionHandler({ Exception.class })
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView processException(Exception exception) {
        logger.info("自定义异常处理-Exception");
        ModelAndView m = new ModelAndView();
        m.addObject("ericException", exception.getMessage());
        m.setViewName("error/500");
        return m;
    }

}

 

在templates/error下新建错误页面500.ftl

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
    <h1>500 error-系统出错,请联系后台管理员</h1>
    <h1>异常信息:${ericException}</h1>
</body>
</html>

 

代码结构:

 

技术分享

 

这三种方式根据需要自行选择,建议使用第三种,更加灵活。

 

Spring Boot Learning(错误处理)

标签:ons   throw   自定义   代码   rri   not   cto   ice   UI   

原文地址:http://www.cnblogs.com/lys0410/p/6537951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!