简体   繁体   中英

Invalid @BsonCreator constructor. Expected class java.lang.String, found interface java.util.List

I'm trying to use POJOs to write data to MongoDB, but I can't seem to get it working. I have a lot of different data structures because I'm storing various types of chart data.

Let's start with the error:

org.bson.codecs.configuration.CodecConfigurationException: Invalid @BsonCreator constructor in BigDecimalDataset. Invalid Property type for 'borderColor'. Expected class java.lang.String, found interface java.util.List.
    at org.bson.codecs.pojo.CreatorExecutable.getError(CreatorExecutable.java:145)
    at org.bson.codecs.pojo.CreatorExecutable.getError(CreatorExecutable.java:135)
    at org.bson.codecs.pojo.ConventionAnnotationImpl.processCreatorAnnotation(ConventionAnnotationImpl.java:200)
    at org.bson.codecs.pojo.ConventionAnnotationImpl.apply(ConventionAnnotationImpl.java:53)
    at org.bson.codecs.pojo.ClassModelBuilder.build(ClassModelBuilder.java:273)
    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)

Here is how I'm creating the registry, and it errors when I .build() the PojoCodecProvider :

            CodecRegistry registry = CodecRegistries.fromRegistries(
                    MongoClientSettings.getDefaultCodecRegistry(),
                    CodecRegistries.fromProviders(
                            PojoCodecProvider.builder()
                                    .register(SystemAMultiSeriesChartData.class)
                                    .register(BigDecimalDataset.class)
                                    .register(SystemASystemInsightsDocument.class)
                                    .register(InsightsDocument.class)
                                    .register(SystemInsights.class)
                                    .register(ChartData.class)
                                    .register(ChartData.Dataset.class)
                                    .register(ChartData.MultiSeriesChartData.class)
                                    .register(GenericInsights.class)
                                    .automatic(true)
                                    .build()
                    )
            );

            mongoClient = MongoClients.create(MongoClientSettings.builder()
                    .codecRegistry(registry)                    
                    .applyToClusterSettings(builder
                            -> builder.hosts(app.getMongoServers()))
                    .build());

Here is the offending class:

public class BigDecimalDataset extends ChartData.Dataset<BigDecimal> {

    public BigDecimalDataset() {
        super(null, null);
    }

    public BigDecimalDataset(String label) {
        super(label, null);
    }

    public BigDecimalDataset(String label, List<BigDecimal> data) {
        super(label, null, data);
    }

    public BigDecimalDataset(String label, String stack, List<BigDecimal> data) {
        super(label, stack, data, new ArrayList<String>(), new ArrayList<String>(), null);
    }

    @BsonCreator
    public BigDecimalDataset(@BsonProperty("label") String label,
            @BsonProperty("stack") String stack,
            @BsonProperty("data") List<BigDecimal> data,
            @BsonProperty("backgroundColor") List<String> backgroundColor,
            @BsonProperty("borderColor") List<String> borderColor,
            @BsonProperty("borderWidth") String borderWidth) {
        super(label, stack, data, backgroundColor, borderColor, borderWidth);
    }
}

And it's superclass:

public static class Dataset<T> {

        @BsonProperty("data")
        protected List<T> data;
        @BsonProperty("label")
        protected String label;
        @BsonProperty("stack")
        protected String stack;
        @BsonProperty("backgroundColor")
        protected List<String> backgroundColor;
        @BsonProperty("borderColor")
        protected List<String> borderColor;
        @BsonProperty("borderWidth")
        protected String borderWidth;

        public Dataset() {
            this(null, null);
        }

        public Dataset(String label) {
            this(label, null);
        }

        public Dataset(String label, List<T> data) {
            this(label, null, data);
        }

        public Dataset(String label, String stack, List<T> data) {
            this.label = label;
            this.stack = stack;
            this.data = ListUtil.emptyListIfNull(data);
            this.borderColor = new ArrayList<>();
            this.backgroundColor = new ArrayList<>();
        }

        @BsonCreator
        public Dataset(@BsonProperty("label") String label,
                @BsonProperty("stack") String stack,
                @BsonProperty("data") List<T> data,
                @BsonProperty("backgroundColor") List<String> backgroundColor,
                @BsonProperty("borderColor") List<String> borderColor,
                @BsonProperty("borderWidth") String borderWidth) {
            this.label = label;
            this.stack = stack;
            this.data = ListUtil.emptyListIfNull(data);
            this.borderColor = borderColor;
            this.backgroundColor = backgroundColor;
            this.borderWidth = borderWidth;
        }

        /**
         * @return the label
         */

    }
}

Why would List be an issue?

UPDATE:

It appears that method overloading is forbidden. Dataset fails because borderColor has two signatures (one being a convenience method):

        public void setBorderColor(List<String> borderColor) {
            this.borderColor = borderColor;
        }

        public void setBorderColor(String borderColor) {
            if (this.borderColor == null) {
                this.borderColor = new ArrayList<>();
            }
            this.borderColor.add(borderColor);
        }

MongoDB's driver get confused on which setBorderColor method to call.

I filed a Jira issue with MongoDB: https://jira.mongodb.org/browse/JAVA-4508

There must be a String getBorderColor() method. Remember that a POJO deals with getter and setters, so they must be consistent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM