找不到匹配的构造函数:java.time.LocalDateTime(java.lang.String)

Could not find matching constructor for: java.time.LocalDateTime(java.lang.String)

我有一个 Grails 3.1.2 应用程序

我的域之一 类 是目标

import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

class Goal {
    String name
    String description
    LocalDateTime targetDate
    Date dateCreated
    Date lastUpdated
    static constraints = {
        name(nullable: false, blank: false)
    }
}

当我对我的目标实例调用验证时,我得到:

如何更改验证,使其不需要此构造函数?

How can I change the validation so it doesn't need this constructor?

你不能,但验证并不是真正的问题。数据绑定器是问题所在。默认活页夹还没有内置对 LocalDateTime.

的支持

您可以像这样注册自己的转换器:

转换器:

// src/main/groovy/demo/MyDateTimeConverter.groovy
package demo

import grails.databinding.converters.ValueConverter

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class MyDateTimeConverter implements ValueConverter {
    @Override
    boolean canConvert(Object value) {
        value instanceof String
    }

    @Override
    Object convert(Object value) {
        def fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
        LocalDateTime.parse(value, fmt)
    }

    @Override
    Class<?> getTargetType() {
        LocalDateTime
    }
}

将其注册为 bean:

// grails-app/conf/spring/resources.groovy
beans = {
    myConverter demo.MyDateTimeConverter
}