BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]

错误提示:

Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()

DTO中包含LocalDateTime.如果有此类型的字段.基本就是400.转化失败.

因为他要求字符串的格式里面有T.就是2017-05-17T12:45:00 这种的.没什么卵用.

于是上网百度.2种办法.1.自定义参数解析器.终极大招.2.是注册转换服务.比较轻巧.就几句代码.也简单.对常用的几种格式进行了转换.

org.springframework.format.support.FormattingConversionServiceFactoryBean

一开始没什么问题.DTO种包含LocalDateTime.成功转换.

但是今天直接用LocalDateTime做参数就报上面那个错误.错误原因很简单.要new一个对象.但是LocalDateTime.不能new.上网也百度了好久.没找到什么好办法.关键是没几个遇到并且提问.....就差使用终极大招了.

最后想了想.他不是要new嘛.

@RequestParam(required = false)

把必须设成false.看行不行.结果瞎猫碰上死耗子.成了.它没有去new对象去.于是就进行到了自定义转换那一步.也就成了.

BeanUtils.instantiateClass()

吐槽一下这个方法.你说自定义的类不能new你报错就算了.这种java自带的也不特殊判断一下.真是日了狗了.....我也没找到什么重写之类的方法.

原文地址:https://www.cnblogs.com/lansehai/p/6867035.html