Modelmapper 无法将 java.lang.String 转换为 java.sql.Timestamp

Modelmapper failed to convert java.lang.String to java.sql.Timestamp

main() {
StudentDataDto source= new StudentDataDto();
    studentDataDto.setCreatedAt("2022-01-20T11:12:46");
    StudentMetaDataEntity destination= modelMapper.map(studentDataDto,
        StudentMetaDataEntity.class);
}

StudentDataDto {
private String createdAt;
}

StudentMetaDataEntity {
private Timestamp createdAt; (java.sql.Timestamp)
}

异常消息:

org.modelmapper.MappingException: ModelMapper mapping errors:

1) Converter org.modelmapper.internal.converter.DateConverter@2b08772d failed to convert java.lang.String to java.sql.Timestamp.
Caused by: org.modelmapper.MappingException: ModelMapper mapping errors:

1) String must be in JDBC format [yyyy-MM-dd HH:mm:ss.fffffffff] to create a java.sql.Timestamp

1 error
    at org.modelmapper.internal.Errors.toMappingException(Errors.java:258)
    at org.modelmapper.internal.converter.DateConverter.dateFor(DateConverter.java:125)
    at org.modelmapper.internal.converter.DateConverter.convert(DateConverter.java:70)
    at org.modelmapper.internal.converter.DateConverter.convert(DateConverter.java:53)
    at org.modelmapper.internal.MappingEngineImpl.convert(MappingEngineImpl.java:306)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:109)
    at org.modelmapper.internal.MappingEngineImpl.setDestinationValue(MappingEngineImpl.java:245)
    at org.modelmapper.internal.MappingEngineImpl.propertyMap(MappingEngineImpl.java:187)
    at org.modelmapper.internal.MappingEngineImpl.typeMap(MappingEngineImpl.java:151)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:114)
    at org.modelmapper.internal.MappingEngineImpl.map(MappingEngineImpl.java:71)
    at org.modelmapper.ModelMapper.mapInternal(ModelMapper.java:573)
    at org.modelmapper.ModelMapper.map(ModelMapper.java:406)
...

参考,我了解到模型映射器的源代码限制了时间戳字符串格式。

我的问题: 不是将我的 StudentDataDto 属性 类型更改为 java.sql.Timestamp,我是否可以在 yyyy-MM-dd'T'HH:mm:ss 中保留我想要的时间戳格式并自定义我的 modelmapper 转换器来解决异常?

您只需要编写自己的转换器并将其注册到 ModelMapper 实例。

  1. 选项 1 - 更一般。如果您的日期字符串将始终采用这种格式,您可以编写一个从 Stringjava.sql.Timestamp 的转换器,这样即使使用其他 dto 也会始终应用它。
public class StringToTimestampConverter implements Converter<String, Timestamp> {

        private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

        @Override
        public Timestamp convert(MappingContext<String, Timestamp> mappingContext) {
            String source = mappingContext.getSource();
            LocalDateTime dateTime = LocalDateTime.parse(source, this.formatter);
            return Timestamp.valueOf(dateTime);
        }
    }

基本上使用 DateTimeFormatter 将字符串转换为 LocaDateTime,然后使用 Timestamp.valueOf(LocalDateTime).

进行转换
  1. 选项 2 - 更具体。如果您在应用中使用不同的格式,您可以将 StudentDataDto 转换为 StudentMetaDataEntity 转换器
public class DtoToMetaConverter implements Converter<StudentDataDto, StudentMetaDataEntity> {

        @Override
        public StudentMetaDataEntity convert(MappingContext<StudentDataDto, StudentMetaDataEntity> mappingContext) {
            StudentDataDto source = mappingContext.getSource();
            StudentMetaDataEntity dest = new StudentMetaDataEntity();
            //Convert string to timestamp like in other example
            dest.setCreatedAt(...);
            return dest;
        }
    }

然后注册转换器并测试它。

public class TimestampMain {

    public static void main(String[] args) {
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.addConverter(new StringToTimestampConverter());

        StudentDataDto source = new StudentDataDto();
        source.setCreatedAt("2022-01-20T11:12:46");
        StudentMetaDataEntity destination = modelMapper.map(source, StudentMetaDataEntity.class);
        System.out.println(destination.getCreatedAt());
    }
}

此示例使用更通用的选项 1,但如果您需要选项 2,只需以类似方式注册另一个转换器。