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 中国大陆许可协议进行许可。
标签:
SpringBoot
, ExceptionHandler
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步