我正在嘗試使用 POJO 將資料寫入 MongoDB,但我似乎無法讓它作業。我有很多不同的資料結構,因為我要存盤各種型別的圖表資料。
讓我們從錯誤開始:
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)
這是我創建注冊表的方式,當我.build()執行以下操作時它會出錯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());
這是有問題的課程:
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);
}
}
它是超類:
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
*/
}
}
為什么會有List問題?
更新:
似乎禁止方法多載。 Dataset失敗是因為borderColor有兩個簽名(一個是方便的方法):
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);
}
setBorderColorMongoDB 的驅動程式對呼叫哪個方法感到困惑。
我向 MongoDB 提交了一個 Jira 問題:https ://jira.mongodb.org/browse/JAVA-4508
uj5u.com熱心網友回復:
必須有String getBorderColor()方法。請記住,POJO 處理 getter 和 setter,因此它們必須保持一致。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/432878.html
標籤:爪哇 mongodb mongodb-java mongo-java-驱动程序
下一篇:如何按月計算新值?
