Spring Boot 使用AOP切面全局处理异常

package com.deity.test.exception;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 使用AOP全局处理异常
 * create by admin at 2018-10-9 23:33:15
 */
@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public String handleGlobalException(Exception exception,Model model){
        model.addAttribute("message",exception.getMessage());
        return "error";
    }
}

在error.html页面显示错误信息,并返回首页

<div>
    <h3><span th:text="${message}"></span></h3>
    <a href="/">前往首页</a>
 </div>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容