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)
}
Exception message:
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)
...
By referring to this similar question's answers, I understand that the source code of model mapper restrict the timestamp string formats.
My question:
Instead of changing my StudentDataDto
property type to java.sql.Timestamp
, is it possible that I keep my desired timestamp format in yyyy-MM-dd'T'HH:mm:ss
and customize my modelmapper converter to solve the exception?
CodePudding user response:
You just need to write you own converter and register it with ModelMapper
instance.
- Option 1 - more general. If your date string will always be in this format, you can write a converter from
String
tojava.sql.Timestamp
, so it will always be applied even when using other dtos.
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);
}
}
Basically convert string to LocaDateTime
using DateTimeFormatter
, then convert using Timestamp.valueOf(LocalDateTime)
.
- Option 2 - more specific. If you are using different formats in your app you can make
StudentDataDto
toStudentMetaDataEntity
converter
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;
}
}
Then register the converter and test it.
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());
}
}
This example uses the more general option 1, but if you need option 2, just register the other converter in similar fashion.