SpringBoot 如何正确拦截thymeleaf异常

SpringBoot 拦截thymeleaf异常

这是一篇可以正确拦截org.thymeleaf.exceptions.TemplateInputException异常的方法水矿石文章,不是解决业务问题的文章,比如不经常见到的

org.thymeleaf.exceptions.TemplateProcessingE
xception: Could not parse as each: "message : xxx " (template: "xxxx" - line xx, col xx)

thymeleaf异常复现

你是故意的,还是不小心的 ----我是故意的

image.png

成功拦截,使用自定义试图,捕获主要信息,减去一大推报错代码,只显示正常信息,显得优雅

SpringBoot 如何正确拦截thymeleaf异常-每日运维
image.png

为什么不生效

首先应该知道 @ControllerAdvice 是拦截不成功的,百世(试)不得其姐(解),正常来说,异常是会走下面这一行代码的

@ControllerAdvice
public class SysGlobalExceptionHandler extends BasicController