Spring Boot自定义错误页
Spring Boot返回的信息类型
Spring Boot在返回错误信息时不一定返回HTML页面,而且根据实际情况返回HTML页面或者一段JSON(若开发者发起Ajax请求,则错误信息时一段JSON)。对于开发者而言,这一段HTML或者JSON都能够自由定制。
BasicErrorController中errorHtml()和error()源码
Spring Boot中的错误默认是由BasicErrorController类来处理的,该类中的核心方法主要有两个:
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = getStatus(request);
Map<String, Object> model = Collections
.unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = resolveErrorView(request, response, status, model);
return (modelAndView != null) ? modelAndView : new ModelAndView(&#