Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Mongo throw CodecConfigurationException: Can't find a codec for CodecCacheKey{clazz=class java.lang.Class, types=null}

When save document that have a property Class<?> catch exception CodecConfigurationException: Can't find a codec for CodecCacheKey{clazz=class java.lang.Class, types=null}

@Document("my_document")
public class MyDocument {

    @Id
    private String id;

    @Field("jPt")
    private JobParameter<?> jobParameter;
}
public class JobParameter<T> implements Serializable {
    private T value;
    private Class<T> type;
    private boolean identifying;
}

Trying added Converter<JobParameter, Document> but it doesn't has effect

    @Bean
    public MongoCustomConversions customConversions(List<Converter<?, ?>> foreignConverters) {
        List<Converter<?, ?>> converters = new ArrayList<>(List.of(
            JSR310DateConverters.ZonedDateTimeToDateConverter.INSTANCE,
            JSR310DateConverters.DateToZonedDateTimeConverter.INSTANCE,
            JSR310DateConverters.LongToDurationConverter.INSTANCE,
            JSR310DateConverters.DurationToLongConverter.INSTANCE,
            **new JobParameterMongoConverter()**
        ));
        converters.addAll(foreignConverters);
        MongoCustomConversions mongoCustomConversions = new MongoCustomConversions(converters);
        MongoCustomConversions.create(adapter -> adapter.useNativeDriverJavaTimeCodecs()
            .registerConverters(converters));
        return mongoCustomConversions;
like image 321
Андрей Дикий Avatar asked Mar 03 '26 17:03

Андрей Дикий


1 Answers

I was not using the correct annotation on the converter. I used @ReadingConverter instead of @WritingConverter.

like image 121
Андрей Дикий Avatar answered Mar 05 '26 05:03

Андрей Дикий



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!