SpringBoot项目org.thymeleaf.exceptions.TemplateInputException: Error resolving template "", template might not exist or might not be accessible by any of the of the configured Template Resolvers

背景:

自定义Exception进行抛出时报错!且不抛异常时返回正常!

Controller层用的注解为@Controller 和每个方法上@ResponsBody组合

而@ExcepotionHandler中 返回结果仅仅为Object 如:

复制代码
@ExceptionHandler(value = DGException.class)
    public Map<String, Object> handleCustomerException(DGException exception) {
        final String stackTrace = ExceptionUtils.getStackTrace(exception);
        Map<String, Object> resultHashMap = new HashMap<>();
        if (null != exception.getMessage() && !"".equals(exception.getMessage())) {
            resultHashMap.put("desc", exception.getMessage());
        } else {
            resultHashMap.put("desc", "系统繁忙,请稍后再试");
        }
        resultHashMap.put("code", -1);
        return resultHashMap;
    }
复制代码

导致返回类型不一致

在自定义异常处对应加上@ResponsBody即可!

即无论是正常返回结果还是自定义异常返回结果保证数据结构的一致性!

本文作者:苍舒

本文链接:https://www.cnblogs.com/cangshublogs/p/10291504.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   苍舒  阅读(270)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起