spring boot 2.0+ 自定义配置404、500页面
1.新建controller类:MyErrorController
package com.example.meeting.controller; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; @Controller public class MyErrorController implements ErrorController { @RequestMapping("/error") public String handleError(HttpServletRequest request){ //获取statusCode:401,404,500 Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); if(statusCode == 401){ return "/error/401"; }else if(statusCode == 404){ return "/error/404"; }else if(statusCode == 403){ return "/error/403"; }else{ return "/error/500"; } } @Override public String getErrorPath() { return "/error"; } }
2.将500.html、404.html放在相应文件夹下:
3.显示效果,随便输入一个不存在的界面:
参考:
转自:https://blog.csdn.net/lazycheerup/article/details/87359689