Reputation: 247
Recently I noticed that our spring-boot services are logging a warning from org.springframework.data.convert.CustomConversions
.
The message is this:
Registering converter from class microsoft.sql.DateTimeOffset to class java.time.OffsetDateTime as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
The relevant dependencies from the pom.xml
are
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.1.jre16</version>
</dependency>
I can of course suppress the logging of the warning by setting the log-level to error
as suggested by answers to the similar question asked here, e.g like this or like this
The entity POJO's date/datetime fields are java.sql.Timestamp
or java.util.Date
and the SQL Server tables have columns of type date
, datetime2
and datetime
.
Mapping from POJO's to SQL is done by org.springframework.data.repository.*/org.springframework.data.jdbc.repository.*
classes.
I would like to know how to correctly configure CustomConversions
so as to avoid the warning altogether, if possible.
Upvotes: 8
Views: 1614
Reputation: 468
You can add a converter and tell spring-data to use it.
First create a converter like this:
import microsoft.sql.DateTimeOffset;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import java.time.OffsetDateTime;
@ReadingConverter
class DateConverter implements Converter<DateTimeOffset, OffsetDateTime> {
@Override
public OffsetDateTime convert(DateTimeOffset source) {
return source.getOffsetDateTime();
}
}
And then use AbstractJdbcConfiguration
to tell spring-data to use the converter. There are many ways to do this but here is an example:
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import java.util.List;
@Configuration
public class DatabaseContext {
@Bean
public AbstractJdbcConfiguration jdbcConfiguration() {
return new AbstractJdbcConfiguration() {
@Override
protected List<?> userConverters() {
return List.of(new DateConverter());
}
};
}
}
Upvotes: 1