一、SpringBoot默认的错误处理机制
默认效果:
1、浏览器,返回一个默认的错误页面。
2、如果是其他客户端,默认响应一个json数据。
3、原理:可以参照ErrorMvcAutoConfiguration;错误处理的自动配置。给容器中添加了以下组件:
(1)、DefaultErrorAttributes:
//帮我们在页面共享信息
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = new LinkedHashMap<String, Object>();
errorAttributes.put("timestamp", new Date());
addStatus(errorAttributes, requestAttributes);
addErrorDetails(errorAttributes, requestAttributes, includeStackTrace);
addPath(errorAttributes, requestAttributes);
return errorAttributes;
}
(2)、BasicErrorController:处理默认/error
请求。
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
@RequestMapping(produces = "text/html") //产生html类型的数据;浏览器发送的请求来到这个方法处理
public ModelAndView errorHtml(HttpServletRequest re