package cn.**.controller; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyException implements ErrorController { private static Logger log = LoggerFactory.getLogger(MyException.class); @RequestMapping("/error") public String handleError(HttpServletRequest request) { log.info("進入異常跳轉"); Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); switch (statusCode) { case 404: log.info("404異常跳轉"); return "fail"; case 403: log.info("403異常跳轉"); return "403"; case 500: log.info("500異常跳轉"); return "/error/500"; default: log.info("默認異常跳轉"); return "/error/404"; } } @Override public String getErrorPath() { return "/error"; } }