我使用Spring Data Rest,我需要添加一个自定义转换器。如果我使用@GetMapping(value =“ / search / findByFooId”)创建一个@RepositoryRestController,我有一个解决方法。我覆盖了WebMVC部分。
第一个转换器:
public class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(final Date source) {
return source == null ? null : ofInstant(source.toInstant(), systemDefault());
}
}
第二个转换器:
public class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
@Override
public Date convert(final ZonedDateTime source) {
return source == null ? null : Date.from(source.toInstant());
}
}
我的春季会议:
@Configuration
public class MyRepositoryRestConfigurerAdapter implements RepositoryRestConfigurer {
@Bean
public DateToZonedDateTimeConverter dateToZonedDateTimeConverter() {
return new DateToZonedDateTimeConverter();
}
@Bean
public ZonedDateTimeToDateConverter zonedDateTimeToDateConverter() {
return new ZonedDateTimeToDateConverter();
}
@Autowired
private CompanyRequestValidatorProxy companyRequestValidatorProxy;
public void configureConversionService(ConfigurableConversionService conversionService) {
System.out.println("--- debug ---");
conversionService.addConverter(dateToZonedDateTimeConverter());
conversionService.addConverter(zonedDateTimeToDateConverter());
}
public void configureRepositoryRestConfiguration(final RepositoryRestConfiguration config) {
config.exposeIdsFor(Foo.class);
}
}
但是我有这个错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.ZonedDateTime] to type [java.util.Date]