When I use Spring Boot's global exception handler, I got this:
org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap' - maybe not public?
This is my code, and I have import the Spring Security and Thymeleaf in my project.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8" />
<title>统一异常处理</title>
</head>
<body>
<h1> 系统异常 </h1>
<div th:text="${url ?: '未捕捉到URL'}"></div>
<div th:text="${exception == null ? '暂无异常信息' : exception.message}"></div>
</body>
</html
@GetMapping("/test")
public String test() throws Exception {
throw new Exception("发生错误");
}
@ControllerAdvice
public class GlobalExceptionHandler {
private static final String DEFAULT_ERROR_VIEW = "/error";
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) {
LOGGER.error("系统异常", e);
ModelAndView mav = new ModelAndView();
mav.addObject("exception", e);
mav.addObject("url", request.getRequestURI());
mav.setViewName(DEFAULT_ERROR_VIEW);
return mav;
}
}
Your view name should be "error" but not "/error", the view resolver would find the template named error.html in the template folder, if the view resolver can not find one it will use the default one, in which the timestamp is needed in models.