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即可!

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

猜你喜欢

转载自www.cnblogs.com/cangshublogs/p/10291504.html
今日推荐