You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

访问DocumentDB记录抛出ArrayIndexOutOfBoundsException问题求助

问题描述

从DocumentDB集群访问记录时抛出如下错误:

org.mongodb.driver.protocol.command: Execution of command with request id 48 failed to complete successfully in 149.61 ms on connection [connectionId{localValue:28}] to server localhost:9200
java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 1
    at org.bson.codecs.pojo.InstanceCreatorImpl.getInstance(InstanceCreatorImpl.java:90)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:122)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:125)
    at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37)
    at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
    at org.bson.internal.LazyCodec.decode(LazyCodec.java:48)
    at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)

Mongo客户端配置:

CodecRegisty codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
                      fromProviders(PojoCodecProvider.builder()
                                                     .automatic(true)
                                                     .build()));


final MongoClientSettings settings = MongoClientSettings.builder()
                                                        .credential(credential)
                                                        .retryWrites(false)
                                                        .applyToSslSettings(builder -> builder
                                                                .enabled(docDbSecret.isSsl())
                                                                .invalidHostNameAllowed(isInvalidHostNameAllowed))
                                                        .applyToClusterSettings(builder -> builder
                                                                .hosts(Collections.singletonList(serverAddress)))
                                                        .codecRegistry(codecRegistry)
                                                        .build();

MongoClient = MongoClients.create(settings);

对应的Document实体类:

@Getter
@Setter
@NoArgsConstructor
@Builder
public final class Document {
    private String a;
    private String b;

    public Document(@BsonProperty String a, @BsonProperty String b) {
        this.a = a;
        this.b = b;
    }
    
    @BsonId
    public String getId() {
       return a+":"+b;
    }
 
}

注:通过Mongo Shell使用find()命令可正常查询该文档。

解决思路
  • 问题根源:实体类的@BsonId注解用法错误,且final类与@Builder的组合导致PojoCodec实例化对象时出现冲突。数据库中的_id字段无法通过动态计算的getId()方法完成反序列化,同时final类限制了Lombok生成Builder子类的逻辑,最终引发数组越界错误。

  • 修复步骤:

    1. 修正@BsonId的使用:
      数据库中的_id字段需要对应实体类的属性,而非动态计算的方法。调整实体类,添加id属性并标记@BsonId:

      @Getter
      @Setter
      @NoArgsConstructor
      @Builder
      public class Document {
          @BsonId
          private String id;
          private String a;
          private String b;
      
          public Document(String id, String a, String b) {
              this.id = id;
              this.a = a;
              this.b = b;
          }
      
          // 若需要保留动态生成ID的逻辑,可添加非注解的方法
          public String generateCombinedId() {
              return a + ":" + b;
          }
      }
      

      插入数据时手动将id设置为a:b格式的字符串,确保与数据库存储一致。

    2. 移除类的final修饰符:
      final类会阻止Lombok生成@Builder所需的内部子类,导致PojoCodec无法正确创建实例,移除该修饰符即可解决实例化冲突。

    3. 优化Codec配置:
      避免automatic(true)带来的自动映射冲突,明确指定需要映射的实体类:

      PojoCodecProvider.builder()
                      .automatic(true)
                      .register(Document.class)
                      .build()
      
    4. 验证文档结构:
      确认数据库中文档的_id字段类型为字符串,且格式与实体类的id属性匹配,避免类型不匹配引发的解码错误。


内容的提问来源于stack exchange,提问作者arvind

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:35:18