相关文章推荐
刚毅的海龟  ·  Could not find ...·  1 月前    · 
想发财的炒粉  ·  MySQL ...·  11 月前    · 
鬼畜的钢笔  ·  基于综合 DNA ...·  1 年前    · 

When I try to fetch record with {"name":undefined} I get following exception.

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.BsonUndefined to type java.lang.String
    at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:176) ~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
    at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:821) ~[spring-data-mongodb-1.7.1.RELEASE.jar:?]

How to solve this? I have multiple types which needs to be converted from BsonUndefined like String, Date, PhoneNumber, etc...

Answers

We ran into this issue recently, so I'm putting the results of my research here in case someone else has this issue as well.

This is a known error in Spring Data MongoDB:

https://jira.spring.io/browse/DATAMONGO-1439

The workaround from that link is to add an explicit converter for converting from BsonUndefined to null. For example, using Java 8:

@ReadingConverter
public class BsonUndefinedToNullObjectConverterFactory implements ConverterFactory<BsonUndefined, Object> {
    @Override
    public <T extends Object> Converter<BsonUndefined, T> getConverter(Class<T> targetType) {
        return o -> null;

Without the Lambda (pre-Java 8):

@ReadingConverter
public class BsonUndefinedToNullObjectConverterFactory implements ConverterFactory<BsonUndefined, Object> {
    @Override
    public <T extends Object> Converter<BsonUndefined, T> getConverter(Class<T> targetType) {
        return new Converter<BsonUndefined, T>() {
            @Override
            public T convert(BsonUndefined source) {
                return null;
   
Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐

  • 浏览量 82
  • 收藏 0
  • 0

所有评论(0)