spring-projects / spring-projects/spring-data-rest
Unable to set custom converters on HttpHeadersPreparer
@odrotbohm is already working on this.
Since Jul 25, 2022.
- Dominant language
- Java
- Stars
- 958
- Forks
- 568
- PR merge metrics
- No merged PRs in 30d
Description
When using spring-data Auditing functionality with unnoficial supported types like OffsetDateTime, you could customize all process using property dateTimeProviderRef on EnableJpaAuditing/EnableMongoAuditing.
All works with no problem for @CreatedDate annotation, but problems comes in with @LastModifiedBy annotation.
Problem in concrete is with class org.springframework.data.rest.webmvc.HttpHeadersPreparer and method getLastModifiedInMilliseconds
private Optional<Long> getLastModifiedInMilliseconds(Object object) {
return getAuditableBeanWrapper(object)//
.flatMap(it -> it.getLastModifiedDate())//
.map(it -> conversionService.convert(it, Date.class))//
.map(it -> conversionService.convert(it, Instant.class))//
.map(it -> it.toEpochMilli());
}
As you can see, conversionService try to convert lastModifiedDate to Date or Instant, the main problem is that conversionService couldn´t be addapted for inclusion of custom converters because it is initilized on HttpHeadersPreparer constructor:
private final ConfigurableConversionService conversionService = new DefaultConversionService();
public HttpHeadersPreparer(AuditableBeanWrapperFactory auditableBeanWrapperFactory) {
Assert.notNull(auditableBeanWrapperFactory, "AuditableBeanWrapperFactory must not be null!");
Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);
this.auditableBeanWrapperFactory = auditableBeanWrapperFactory;
}
It would be nice to reach that conversionService for registering custom converters
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.