Skip to content

Custom converter for multibuckets  #1141

Closed
@moriyahazan

Description

@moriyahazan

i implemented multibuckets according to explained in this issue but i can't add custom converters to all buckets, the converters added for default bucket only

please see my code:

Custome converter

 @Override
   public CouchbaseCustomConversions customConversions() {
       return new CouchbaseCustomConversions(Arrays.asList(
               ZonedDateTimeToEpochTimeConverter.INSTANCE,
               EpochTimeToZonedDateTimeConverter.INSTANCE));
   }


   @WritingConverter
   public enum ZonedDateTimeToEpochTimeConverter implements Converter<ZonedDateTime, CouchbaseDocument> {
       INSTANCE;

       @Override
       public CouchbaseDocument convert(ZonedDateTime zonedDateTime) {
           CouchbaseDocument cd = new CouchbaseDocument();
           cd.put(EPOC_MILLI, zonedDateTime.toInstant().toEpochMilli());
           cd.put(OFFSET_SECONDS, zonedDateTime.getOffset().getTotalSeconds());
           return cd;
       }
   }

   @ReadingConverter
   public enum EpochTimeToZonedDateTimeConverter implements Converter<CouchbaseDocument, ZonedDateTime> {
       INSTANCE;

       @Override
       public ZonedDateTime convert(CouchbaseDocument epochTime) {
           long timeMilli = Long.parseLong(epochTime.getContent().get(EPOC_MILLI).toString());
           int offsetSeconds = Integer.parseInt(epochTime.getContent().get(OFFSET_SECONDS).toString());

           ZoneOffset convertedOffset = ZoneOffset.ofTotalSeconds(offsetSeconds);
           return ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMilli), convertedOffset);
       }
   }

add converter to the costum mapping:


MappingCouchbaseConverter mappingCouchbaseConverter = new MappingCouchbaseConverter();
mappingCouchbaseConverter.setCustomConversions(customConversions());
baseMapping.mapEntity(Class.forName(beanDefinition.getBeanClassName()),myCouchbaseTemplate(myCouchbaseClientFactory("mybucket"),mappingCouchbaseConverter ));

I am getting below exception when save the doc although there is convertor from zonedatetime to couchebasedocument


2021-05-27 17:29:27.178 ERROR 18916 --- [nio-9001-exec-1] g.k.e.error.DefaultGraphQLErrorHandler   : Error executing query Exception while fetching data (/createProduct) : No converter found capable of converting from type [java.time.ZonedDateTime] to type [org.springframework.data.couchbase.core.mapping.CouchbaseDocument]

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.ZonedDateTime] to type [org.springframework.data.couchbase.core.mapping.CouchbaseDocument]
   at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.5.jar:5.3.5]
   at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.5.jar:5.3.5]
   at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.3.5.jar:5.3.5]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.lambda$getPotentiallyConvertedSimpleWrite$2(MappingCouchbaseConverter.java:784) ~[spring-data-couchbase-4.1.6.jar:4.1.6]
   at java.base/java.util.Optional.map(Optional.java:265) ~[na:na]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.getPotentiallyConvertedSimpleWrite(MappingCouchbaseConverter.java:784) ~[spring-data-couchbase-4.1.6.jar:4.1.6]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.writeSimpleInternal(MappingCouchbaseConverter.java:774) ~[spring-data-couchbase-4.1.6.jar:4.1.6]
   at org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter.access$200(MappingCouchbaseConverter.java:82) ~[spring-data-couchbase-4.1.6.jar:4.1.6]

Can you please help?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions