1. 程式人生 > >springBoot(7):web開發-錯誤處理

springBoot(7):web開發-錯誤處理

spring boot 錯誤處理

處理方式一:實現ErrorController接口

原理:Spring Boot 將所有的錯誤默認映射到/error, 實現ErrorController接口

代碼:

package com.example.demo.controller;

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

/**
 * Created by ly on 2017/6/17.
 */
@Controller
@RequestMapping("error")
public class BaseErrorController implements ErrorController {
    @Override
    public String getErrorPath() {
        return "error/error";
    }

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


error.ftl:

<!DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
   <h1>error-系統出錯,請聯系後臺管理員</h1>
</body>
</html>

技術分享


在瀏覽器中輸入一個不存在的URL,效果如下:

技術分享

---------------------------------------------分割線---------------------------------------------

處理方式二:添加自定義的錯誤頁面

對於html靜態頁面:

在resources/public/error/ 下定義

如添加404頁面:resources/public/error/404.html頁面,中文註意頁面編碼


對於模板引擎頁面:

在templates/error/下定義

如添加5xx頁面:templates/error/5xx.ftl


註:templates/error/ 這個的優先級比較 resources/public/error/高

技術分享

效果:此處輸入不存在的URL,則訪問我們的404.hmtl;如果拋出異常,則訪問我們的5xx.ftl

技術分享

技術分享

---------------------------------------------分割線---------------------------------------------

處理方式三:[email protected](全局異常處理)


ExcepitonHandler.java

package com.example.demo.handler;

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;

/**
 * Created by ly on 2017/6/17.
 */
@ControllerAdvice
public class ExcepitonHandler {
    /**
     * 統一異常處理
     *
     * @param exception
     *            exception
     * @return
     */
    @ExceptionHandler({ RuntimeException.class })
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView processException(RuntimeException exception) {
        System.out.println("自定義異常處理-RuntimeException");
        ModelAndView m = new ModelAndView();
        m.addObject("roncooException", exception.getMessage());
        m.setViewName("error/500");
        return m;
    }

    /**
     * 統一異常處理
     *
     * @param exception
     *            exception
     * @return
     */
    @ExceptionHandler({ Exception.class })
    @ResponseStatus(HttpStatus.OK)
    public ModelAndView processException(Exception exception) {
        System.out.println("自定義異常處理-Exception");
        ModelAndView m = new ModelAndView();
        m.addObject("roncooException", exception.getMessage());
        m.setViewName("error/500");
        return m;
    }

}


500.ftl:

<!DOCTYPE html>
<html>
<head lang="en">
   <title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
   <h1>500-系統錯誤</h1>
   <h1>${roncooException}</h1>
</body>
</html>


測試:輸入一個會拋異常的URL

技術分享



本文出自 “我愛大金子” 博客,請務必保留此出處http://1754966750.blog.51cto.com/7455444/1939359

springBoot(7):web開發-錯誤處理