Search code examples
springmongodbspring-dataspring-data-mongodb

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type


UserProfileModel has two embedded models. I am giving here concisely:

@Document(collection = "USER_PROFILE")
public class UserProfileModel extends BaseModel<UserModel>  {

    public static final String FIELD_USER_ID = "userId";
    public static final String FIELD_BASIC_INFO = "basicInfo";
    public static final String FIELD_CONTACT_INFO = "contactInfo";

    private String userId;
    private BasicInfo basicInfo;
    private ContactInfo contactInfo;
}

public class BasicInfo {
    private String gender;
    private LocalDate birthDate;
    private String[] langs;
    private String religiousView;
    private String politicalView;
}

public class ContactInfo {
    private String[] mobilePhones;
    private Address address;
    private SocialLink[] socialLinks;
    private String[] websites;
    private String[] emails;
}

Writing converter class of UserProfileModel:

@Component
@WritingConverter
public class UserProfileModelConverter implements Converter<UserProfileModel, Document> {

    @Override
    public Document convert(UserProfileModel s) {
        Document doc = new Document();

        if (null != s.getUserId())
            doc.put(UserProfileModel.FIELD_USER_ID, s.getUserId());

        if (null != s.getBasicInfo())
            doc.put(UserProfileModel.FIELD_BASIC_INFO, s.getBasicInfo());

        if (null != s.getContactInfo())
            doc.put(UserProfileModel.FIELD_CONTACT_INFO, s.getContactInfo());
    }
}

Trying to save an object of UserProfileModel like this:

@Autowired
private UserProfileRepository repo;

UserProfileModel profileModel = generateUserProfileModel();
repo.save(profileModel);

Exception:

org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when encoding using the AutomaticPojoCodec.
Encoding a BasicInfo: 'BasicInfo(gender=Male, birthDate=2020-05-05, langs=[Lang 1, Lang 2], religiousView=Islam (Sunni), politicalView=N/A)' failed with the following exception:

Failed to encode 'BasicInfo'. Encoding 'langs' errored with: Can't find a codec for class [Ljava.lang.String;.

A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.

    at org.bson.codecs.pojo.AutomaticPojoCodec.encode(AutomaticPojoCodec.java:53)
    at org.bson.codecs.EncoderContext.encodeWithChildContext(EncoderContext.java:91)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:185)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:141)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)

If I don't use converter for UserProfileModel by adding in Mongo Config, then this exception doesn't appear and everything works well.

But I am trying to use the Converter class for some reason.

So is it something wrong or modification needed in converter class?


Solution

  • A Jira ticket already raised on this issue here.

    Can't find a codec for class [Ljava.lang.String;

    And their reply

    The Document class currently supports only List, not native Java array, so just replace with:

    List codes = Arrays.asList("1112233", "2223344");

    So, String array is not supported. Use List<String> instead of String[] in models.