package com.zm.blog.config.advice;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @author Hiiso 拦截异常并统一处理
*/
@ControllerAdvice
public class ExceptionHandlerAdvice {
@ExceptionHandler(value = Exception.class) // 2
public ModelAndView exception(Exception exception, WebRequest request) {
ModelAndView modelAndView = new ModelAndView("/error");// error页面
modelAndView.addObject("errorMessage", exception.getMessage());
return modelAndView;
}
@ModelAttribute // 3
public void addAttributes(Model model) {
model.addAttribute("msg", "额外信息"); // 3
}
}
spring boot爬坑之旅途--拦截异常并统一处理(7)
最新推荐文章于 2021-01-08 13:03:44 发布