簡體   English   中英

春季啟動2:ConverterNotFoundException:未找到能夠從類型[java.time.ZonedDateTime]轉換為類型[java.util.Date]的轉換器

[英]Spring boot 2 : ConverterNotFoundException: No converter found capable of converting from type [java.time.ZonedDateTime] to type [java.util.Date]

我使用spring-boot 2。

我是Junit測試( @SpringBootTest ),我使用org.springframework.test.web.servlet.MockMvc

@Test
@WithMockUser(roles = {"ADMIN"})
public void testGetSearchByFoodIdWithAdmin() throws Exception {
    final ResultActions resultActions = mockMvc.perform(get(RESOURCE_URL + "/search/findByFooId?fooId=123"))
                .andExpect(status().isOk());
}

我有這個轉換器:

public class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
    @Override
    public Date convert(final ZonedDateTime source) {
        return source == null ? null : Date.from(source.toInstant());
    }
}

我有另一個轉換器:

public class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
    @Override
    public ZonedDateTime convert(final Date source) {
        return source == null ? null : ofInstant(source.toInstant(), systemDefault());
    }
}

我有這個Spring配置:

@Configuration
@EnableMongoAuditing(dateTimeProviderRef = "dateTimeProvider")
public class MongoConfiguration {

    @Autowired
    MongoDbFactory mongoDbFactory;

    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new DateToZonedDateTimeConverter());
        converters.add(new ZonedDateTimeToDateConverter());
        return new MongoCustomConversions(converters);
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MappingMongoConverter converter = getDefaultMongoConverter();
        //converter.afterPropertiesSet();
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);
        return mongoTemplate;
    }

    @Bean
    public MappingMongoConverter getDefaultMongoConverter() throws Exception {
        MappingMongoConverter converter = new MappingMongoConverter(
                new DefaultDbRefResolver(mongoDbFactory), new MongoMappingContext());
        converter.setCustomConversions(customConversions());
        return converter;
    }

}

我有這個其他的Spring配置:

@Component
public class MyWebMvcConfigurer extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new DateToZonedDateTimeConverter());
        registry.addConverter(new ZonedDateTimeToDateConverter());
    }

}

我有這個錯誤:

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]

為什么對於MongoDB來說,WebMvc部分沒有考慮我的轉換器,但它很好呢?

編輯:

我使用Spring Data Rest

@RepositoryRestResource(collectionResourceRel = "foo", path = "foo")
public interface FooRepository extends MongoRepository<Foo, String> {

    Foo findByFooId(@Param("fooId") String fooId);

}

我的Foo課程:

@Document
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Foo {

    @Id
    private String keyid;

    @NotNull
    private String fooId;

    @CreatedDate
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private ZonedDateTime creationDate;

    @LastModifiedDate
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private ZonedDateTime updateDate;
}

編輯n°2:

我嘗試使用CustomizedRestMvcConfiguration實現RepositoryRestConfigurer`,但不是我唯一的問題:

@Configuration
@EnableWebMvc
public class CustomizedRestMvcConfiguration implements RepositoryRestConfigurer {

    @Bean
    public DateToZonedDateTimeConverter dateToZonedDateTimeConverter() {
        return new DateToZonedDateTimeConverter();
    }

    @Bean
    public ZonedDateTimeToDateConverter zonedDateTimeToDateConverter() {
        return new ZonedDateTimeToDateConverter();
    }

    @Override
    public void configureConversionService(ConfigurableConversionService conversionService) {
        conversionService.addConverter(dateToZonedDateTimeConverter());
        conversionService.addConverter(zonedDateTimeToDateConverter());
    }

}

MyWebMvcConfigurer擴展了WebMvcConfigurerAdapter ,從Spring 5.0開始不推薦使用 相反,您應該實現WebMvcConfigurer接口:

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        // ...
    }
}

有關更多信息,請參見類型轉換文檔。 還要注意不同的注釋!

是的,sgrillon答案不起作用,因為問題不在Spring轉換中,而是在MongoDB的Jackson JSON轉換器中。

@Document注釋+ ZonedDateTime是問題。 該解決方案如建議的那樣:一個“轉換器”,而不是一個Spring Converter / HandlerMapping。 您需要將其添加到MongoTemplate。

在這里檢查以供參考。 有很多-只是谷歌為:MongoDB ZonedDateTime。

MongoDB的ZonedDateTime

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM