SpringBoot错误页面
springboot 访问不存在的url时会返回错误信息。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Dec 13 15:48:28 CST 2022
There was an unexpected error (type=Not Found, status=404).
No message available
错误信息中标识 ‘This application has no explicit mapping for /error’,应用没有显示的映射/error。
可以重写/error处理方法,代码如下。
@Controller
@Slf4j
public class ErrorController extends BasicErrorController {
public ErrorController(ServerProperties serverProperties) {
super(new DefaultErrorAttributes(), serverProperties.getError());
}
/**
* 覆盖默认的HTML响应
*/
@Override
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("serverExeception");
return modelAndView;
}
}