小编des*_*air的帖子

编码对象时未使用MongoDB BSON编解码器

我正在尝试将对象存储在MongoDB数据库中(使用MongoDB 3.0.2)并CodecConfigurationException在尝试使用错误消息对对象进行编码时获取

Can't find a codec for class java.time.LocalDate. 
Run Code Online (Sandbox Code Playgroud)

我已经编写并包含了LocalDate对象的编解码器.细节如下.

DutyBlock我试图存储的对象具有以下成员变量:

public class DutyBlock {
    private LocalDate startDate;
    private LocalDate endDate; //Inclusive
    private int blockLength;
    private double pointValue;
    private ArrayList<Ra> assigned;
}
Run Code Online (Sandbox Code Playgroud)

我编写了以下编解码器来编码DutyBlock数据库中的对象:

public class DutyBlockCodec implements Codec<DutyBlock> {

    @Override
    public void encode(BsonWriter writer, DutyBlock t, EncoderContext ec) {
        Document document = new Document();
        document.append("startDate", t.getStartDate());
        document.append("endDate", t.getEndDate());
        document.append("blockLength", t.getBlockLength());
        document.append("pointValue", t.getPointValue());
        document.append("assigned", t.getRasOnDuty());

        writer.writeString(document.toJson());  //Line 27 in the error message.
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

java encoding mongodb bson

26
推荐指数
1
解决办法
2万
查看次数

标签 统计

bson ×1

encoding ×1

java ×1

mongodb ×1