2017-04-13 39 views
0

我添加了一个用于Instant < =>Long的couchbase转换器,但是在读取该值时出现错误。Couchbase转换器弹簧数据混淆整数长

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [java.time.Instant]

我的转换器看起来像这样

@WritingConverter 
    public enum InstantToLongConverter implements Converter<Instant, Long> { 
    INSTANCE; 

    public Long convert(Instant source) { 
     return source == null ? null : source.getEpochSecond(); 
    } 
    } 

    @ReadingConverter 
    public enum LongToInstantConverter implements Converter<Long, Instant> { 
    INSTANCE; 

    @Override 
    public Instant convert(Long source) { 
     return source == null ? null : Instant.ofEpochSecond(source); 
    } 
    } 

我应该只使用整型? 这是一个错误?

回答

1

如果你看看java.lang.Integer和java.lang.Long,它们的共同祖先是java.lang.Number。意思是,你的转换器长< =>即时不能被Spring数据用来转换整数。

可能的解决方案:

  • 修改您的转换为数字<转换=>即时
  • 创建另一个转换器整数< =>即时
-1

以下是关于方法重载:

整数从来没有必然长,因为整数和长有不同的对象类型并没有IS-A它们之间的关系。对于任何两个包装类都是如此。

但是,它可以绑定到对象,因为整数IS-A对象。