美文网首页
Spring Boot 使用AOP切面全局处理异常

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

作者: 天神Deity | 来源:发表于2018-10-09 23:36 被阅读18次
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>

相关文章

网友评论

      本文标题:Spring Boot 使用AOP切面全局处理异常

      本文链接:https://www.haomeiwen.com/subject/tgjuaftx.html