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异常复现

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

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

为什么不生效

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

java 复制代码
@ControllerAdvice
public class SysGlobalExceptionHandler<T> extends BasicController<Class<?>>{
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> exception(Throwable e) {
    // 处理异常
    return ResponseEntity.status(this.response.getStatus()).body("Exception未定义异常" + e.getMessage());
    }
}

但是他是不正常的(因为拦截不到),为什么,我也不知道,但是AI是这么说的:@ControllerAdvice只能处理通过控制器抛出的异常,并不能处理其他组件抛出的异常。如果需要处理其他组件的异常,需要使用其他的异常处理机制。 听君一席话,如菜鸟开大

怎么实现拦截生效呢

当然是重新实现 ErrorController接口了

java 复制代码
public Object errorHandle(){
    //判断状态码是500
    if (this.response.getStatus() == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
    // 获取异常信息
    Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
    // 判断是不是模板异常
    if (throwable.getCause() instanceof TemplateInputException) {
        //这里其实也可以是TemplateInputException,throwable.getCause() instanceof TemplateInputException也可以是TemplateProcessingException,最终都是抛出TemplateProcessingException异常
        TemplateProcessingException templateProcessingException = (TemplateProcessingException) throwable.getCause();
        ParseException parseException = (ParseException) templateProcessingException.getCause();
        //这里就是自定义逻辑了
        this.msgFormat("模板出现异常 %s ", parseException.getLocalizedMessage());
        modelAndView.setViewName("err");
        // return this.Output(throwable.getClass());
        //return ResponseEntity.status(this.response.getStatus()).body(parseException.getLocalizedMessage());
    }
}

// this.response就是抛出原来的结果,就比如你写的逻辑没有成功拦截到500状态码的其他异常,是啥就显示啥
return this.response;

}

这样就完成实现拦截到thymeleaf的异常,@ControllerAdvice处理不了的异常可以使用ErrorController来处理,耶稣也拦不住,鲁某说的

有很多东西是后知后觉的,一年前我可能不知道怎么处理,只能去处理业务,避免出现这种异常,时间久了,好多知识点就慢慢领悟了。(多动手)

相关推荐
J_liaty8 小时前
SpringBoot + EMQX:打造物联网设备数据双向通讯的完整解决方案
spring boot·物联网·emqx
Coder_Boy_9 小时前
基于SpringAI的在线考试系统-考试系统DDD(领域驱动设计)实现步骤详解
java·数据库·人工智能·spring boot
crossaspeed10 小时前
Java-SpringBoot的启动流程(八股)
java·spring boot·spring
这儿有个昵称10 小时前
互联网大厂Java面试场景:从Spring框架到微服务架构的提问解析
java·spring boot·微服务·kafka·grafana·prometheus·数据库优化
Coder_Boy_12 小时前
基于SpringAI的在线考试系统-DDD(领域驱动设计)核心概念及落地架构全总结(含事件驱动协同逻辑)
java·人工智能·spring boot·微服务·架构·事件驱动·领域驱动
小北方城市网13 小时前
SpringBoot 集成 RabbitMQ 实战(消息队列解耦与削峰):实现高可靠异步通信
java·spring boot·python·微服务·rabbitmq·java-rabbitmq·数据库架构
程序员老徐13 小时前
SpringBoot嵌入Tomcat注册Servlet、Filter流程
spring boot·servlet·tomcat
guslegend13 小时前
第1章:快速入门SpringBoot
spring boot
Coder_Boy_14 小时前
基于SpringAI的在线考试系统-考试模块前端页面交互设计及优化
java·数据库·人工智能·spring boot
李慕婉学姐14 小时前
Springboot旅游景点管理系统2fj40iq6(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端