Multiple Inner classes POJO - Mongo DB

14 views Asked by At

I am trying below way to deserialize documents being read from mongodb collections

@Data
@ToString(doNotUseGetters = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class FileInfoObject {

    public class Info {
        @BsonProperty("partnerId")
        private String partnerId;
        @BsonProperty("jobId")
        private String jobId;
        @BsonProperty("bucketId")
        private String bucketId;

        @BsonCreator
        public Info(
                @BsonProperty("partnerId") String partnerId,
                @BsonProperty("jobId") String jobId,
                @BsonProperty("bucketId") String bucketId) {
            this.partnerId = partnerId;
            this.jobId = jobId;
            this.bucketId = bucketId;
        }
    }

    // Inner class for "info" object
    public static class FileInfo {

        @BsonProperty("File Name")
        private String fileName;
        @BsonProperty("Algorithm Name")
        private String algorithmName;
        @BsonProperty("Algorithm Type")
        private String algorithmType;
        @BsonProperty("Date")
        private String date;
        @BsonProperty("random")
        private String random;
        @BsonProperty("author")
        private String author;
        @BsonProperty("_class")
        private String _class;
        @BsonCreator
        public FileInfo() {

        }

        public FileInfo(@BsonProperty("File Name") String fileName,
                    @BsonProperty("Algorithm Name") String algorithmName,
                    @BsonProperty("Algorithm Type") String algorithmType,
                    @BsonProperty("Date") String date,
                    @BsonProperty("random") String random,
                    @BsonProperty("author") String author,
                    @BsonProperty("_class") String _class) {
            this.fileName = fileName;
            this.algorithmName = algorithmName;
            this.algorithmType = algorithmType;
            this.date = date;
            this.random = random;
            this.author = author;
            this._class = _class;
        }
    }

    private Object _id;
    @BsonProperty("readTime")
    private Date readTime;
    private Info info;
    private FileInfo fileInfo;

}

However keep seeing below error

org.bson.codecs.configuration.CodecConfigurationException: Invalid @BsonCreator constructor in Info. All parameters in the @BsonCreator method / constructor must be annotated with a @BsonProperty.

at org.bson.codecs.pojo.CreatorExecutable.getError(CreatorExecutable.java:147)
at org.bson.codecs.pojo.CreatorExecutable.getError(CreatorExecutable.java:135)
at org.bson.codecs.pojo.ConventionAnnotationImpl.processCreatorAnnotation(ConventionAnnotationImpl.java:159)
at org.bson.codecs.pojo.ConventionAnnotationImpl.apply(ConventionAnnotationImpl.java:54)
at org.bson.codecs.pojo.ClassModelBuilder.build(ClassModelBuilder.java:272)
at org.bson.codecs.pojo.PojoCodecProvider.createClassModel(PojoCodecProvider.java:219)
at org.bson.codecs.pojo.PojoCodecProvider.access$100(PojoCodecProvider.java:41)
at org.bson.codecs.pojo.PojoCodecProvider$Builder.build(PojoCodecProvider.java:119)
0

There are 0 answers