【问题标题】:(Aggregation) Can't find a codec for class org.springframework.data.mongodb.core.geo.GeoJsonPoint(聚合)找不到类 org.springframework.data.mongodb.core.geo.GeoJsonPoint 的编解码器
【发布时间】:2017-10-20 16:59:13
【问题描述】:

您能帮我解决以下问题吗?

我在下面给出的存储库中有上述错误

@Repository("polygonQueryRepository")
@RequiredArgsConstructor
public class PolygonQueryRepositoryImpl extends PolygonRepository {
    @Autowired
    private MongoOperations operations;

    public List<DBObject> findPolygonsMatchingGivenPointAndInputAggregate(Double lat, Double lng, String band) {
        GeoJsonPoint point = new GeoJsonPoint(lat, lng);
        MatchOperation operation = match(Criteria.where("location").intersects(point).and("attributes.band").regex(band));
        Aggregation aggregation = newAggregation(Polygon.class,
                unwind("attributes.transponders"),
                operation);
        AggregationResults<DBObject> result = 
        operations.aggregate(aggregation,"Polygon",DBObject.class);
        return result.getMappedResults();
    }
}

我之前读过在定义 MongoTemplate bean 时需要注册编解码器。所以我做了 - 仍然没有运气。

@Configuration
public class SpringMongoConfig {

    @Value("${mongo.db.name}")
    private String dbName;

    @Value("${mongo.db.host}")
    private String dbHost;

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        MongoClientOptions options = MongoClientOptions.builder().codecRegistry(MongoClient.getDefaultCodecRegistry()).build();
        return new SimpleMongoDbFactory(new MongoClient(dbHost,options), dbName);
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }
}

你有什么建议吗?

【问题讨论】:

  • 非常不言自明。 MongoDB 不使用GeoJsonPoint 数据结构。由于这是一个自定义函数或至少在外部库中实现,因此您要么需要使用序列化程序进行扩展,要么(在这里有些意义)只需提供您应该执行的预期 BSON 结构。所以建议使用后者,特别是因为“所说的函数”实际上的顺序不正确,GeoJSON 的实际顺序是longitude,然后是latitude。如果没有正确的顺序,MongoDB 查询将无法工作。
  • 事实上 Mongo 以这种方式准确识别 - 经度然后纬度。 GeoJsonPoint 来自 org.springframework.data.mongodb.core.geo - 所以它与 Mongo 和 Spring 的集成非常相关。

标签: java spring mongodb aggregation


【解决方案1】:

你已经接近这一行了:

MongoClientOptions options = MongoClientOptions
    .builder()
    .codecRegistry(MongoClient.getDefaultCodecRegistry())
    .build();

但是要使用GeoJsonPoint类需要实现Codec接口,然后通过如下方式注册:

CodecRegistry registry = CodecRegistries.fromRegistries(
    CodecRegistries.fromCodecs(new GeoJsonPointCodec()), // the codec you need to implement
    MongoClient.getDefaultCodecRegistry()
);

您可以在选项生成器中使用此注册表。

【讨论】:

  • 非常感谢您的评论!我会努力关注它并返回结果。
  • 不幸的是,我没有运气。在实施 Codec 并按照您的建议进行注册后,我仍然发现相同的错误。我想在某个地方decoder 对象被默认的编解码器覆盖。
  • 您使用哪个版本的 Spring Data?我会看看这个。
  • 这是 spring-data-mongodb 1.10.6 spring-data-commons 1.13.6
  • 你能展示这种情况的解码器示例吗?谢谢。
【解决方案2】:

cbartosiak,非常感谢您的回答!这对我帮助很大。但是解决方法是超级超级不明显!:)

实际上需要注册但我无法注册直到我将聚合接口更改为

AggregationResults<AggregatedPolygon> result = template.aggregate((TypedAggregation<?>) aggregation, AggregatedPolygon.class);

所以我只需要向TypedAggregation 对象添加强制转换aggregation 并从接口中删除集合名称。

这真的很奇怪,但它对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2017-04-07
    • 2018-11-01
    • 1970-01-01
    • 2018-04-14
    • 2019-08-31
    • 2023-03-17
    相关资源
    最近更新 更多