【问题标题】:Failed to convert property value of type java.lang.String to required type java.time.LocalDateTime无法将 java.lang.String 类型的属性值转换为所需的 java.time.LocalDateTime 类型
【发布时间】:2019-06-20 01:47:33
【问题描述】:

我的问题有很多类似的问题,但我没有找到解决方案。 实体片段:

@DateTimeFormat
private LocalDateTime valid_from;

@DateTimeFormat
private LocalDateTime valid_to;

我的表格格式为 yyyy-MM-dd。我已经尝试过注释 @DateTimeFormat(format="yyyy-MM-dd") 和 ISO。我试过了:

 @InitBinder
public void initBinder(WebDataBinder webDataBinder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(true);
    webDataBinder.registerCustomEditor(LocalDateTime.class, new CustomDateEditor(dateFormat, true));
}

还有:

@Converter(autoApply = false)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, String> {

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

@Override
public String convertToDatabaseColumn(LocalDateTime locDate) {
    return (locDate == null ? null : formatter.format(locDate));
}

@Override
public LocalDateTime convertToEntityAttribute(String dateValue) {
    return (dateValue == null ? null : LocalDateTime.parse(dateValue, formatter));
}

但我仍然有绑定错误:

无法将 java.lang.String 类型的属性值转换为属性 valid_from 所需的 java.time.LocalDateTime 类型;嵌套异常是 org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.String] 转换为类型 [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] 值 2019-01- 20;嵌套异常是 java.lang.IllegalArgumentException:值 [2019-01-20] 的解析尝试失败

【问题讨论】:

    标签: java spring data-binding


    【解决方案1】:

    为什么不在你的实体和数据库中使用 sql Date,你的列应该是没有时区的时间戳而不是字符串。如果你需要日期格式,为什么不使用 LocalDate 而不是 LocalDateTime?如果您使用 LocalDate,则很容易转换为 sql Date。 :)

    【讨论】:

    • 在这个表格中我只需要日期。但在其他情况下,我也需要时间。我在数据库中的列是时间戳而不是文本。我更喜欢 LocalDateTime 而不是 sql Date api。
    【解决方案2】:

    您可以添加自定义序列化器和反序列化器来完成您想做的事情。像下面这样定义一个序列化器。

    public class MyDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    
        private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    
        @Override
        public DateTime deserialize(final JsonParser jsonParser,
                                    final DeserializationContext deserializationContext) throws IOException {
            String dateStr = null;
            String timeStr = null;
            String fieldName = null;
    
            dateStr = jsonParser.getValueAsString();
            if (dateStr != null) {
    
                return LocalDateTime.parse(dateStr, formatter);
            }
            return null;
         }
    }
    

    您可以编写如下的反序列化器。

    public class MyDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    
        private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime();
    
        @Override
        public void serialize(final DateTime dateTime,
                              final JsonGenerator jsonGenerator, 
                              final SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(formatter.print(dateTime));
        }   
    }
    

    现在您可以如下注释您的日期时间字段。

    @JsonSerialize(using = MyDateTimeSerializer.class)
    @JsonDeserialize(using = MyDateTimeDeserializer.class)
    private LocalDateTime valid_to;
    

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 1970-01-01
      • 2017-05-12
      • 2018-08-21
      • 2017-04-21
      • 2021-01-23
      • 2018-10-13
      • 2017-06-14
      • 2016-07-15
      相关资源
      最近更新 更多