(聚合)找不到 class org.springframework.data.mongodb.core.geo.GeoJsonPoint 的编解码器
(Aggregation) Can't find a codec for class org.springframework.data.mongodb.core.geo.GeoJsonPoint
你能帮我解决以下问题吗?
我在下面给出的存储库中有上面的错误
@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;
}
}
你有什么建议吗?
你接近这条线:
MongoClientOptions options = MongoClientOptions
.builder()
.codecRegistry(MongoClient.getDefaultCodecRegistry())
.build();
但要使用 GeoJsonPoint class 你需要实现 Codec 接口,然后按以下方式注册它:
CodecRegistry registry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(new GeoJsonPointCodec()), // the codec you need to implement
MongoClient.getDefaultCodecRegistry()
);
您可以在选项生成器中使用此注册表。
cbartosiak,非常感谢您的回答!这对我帮助很大。但是解决方法非常非常不明显!:)
事实上,注册表是必需的,但我无法注册,直到我将聚合界面更改为
AggregationResults<AggregatedPolygon> result = template.aggregate((TypedAggregation<?>) aggregation, AggregatedPolygon.class);
所以我只需要将转换添加到 TypedAggregation
aggregation
对象并从接口中删除集合名称。
这真的很奇怪,但它对我有用。
你能帮我解决以下问题吗?
我在下面给出的存储库中有上面的错误
@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;
}
}
你有什么建议吗?
你接近这条线:
MongoClientOptions options = MongoClientOptions
.builder()
.codecRegistry(MongoClient.getDefaultCodecRegistry())
.build();
但要使用 GeoJsonPoint class 你需要实现 Codec 接口,然后按以下方式注册它:
CodecRegistry registry = CodecRegistries.fromRegistries(
CodecRegistries.fromCodecs(new GeoJsonPointCodec()), // the codec you need to implement
MongoClient.getDefaultCodecRegistry()
);
您可以在选项生成器中使用此注册表。
cbartosiak,非常感谢您的回答!这对我帮助很大。但是解决方法非常非常不明显!:)
事实上,注册表是必需的,但我无法注册,直到我将聚合界面更改为
AggregationResults<AggregatedPolygon> result = template.aggregate((TypedAggregation<?>) aggregation, AggregatedPolygon.class);
所以我只需要将转换添加到 TypedAggregation
aggregation
对象并从接口中删除集合名称。
这真的很奇怪,但它对我有用。