我正在使用Spring Boot和mongodb创建一个应用程序。我正在使用地理空间查询,但无法通过Box进行查询。调试时,方法accessor.getGeoNearLocation()
始终返回null。堆栈跟踪为:
java.lang.IllegalArgumentException: Point must not be null!
at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.data.mongodb.core.query.NearQuery.<init>(NearQuery.java:193) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.mongodb.core.query.NearQuery.near(NearQuery.java:254) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.mongodb.core.query.NearQuery.near(NearQuery.java:237) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.mongodb.repository.query.MongoQueryExecution$GeoNearExecution.doExecuteQuery(MongoQueryExecution.java:157) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.mongodb.repository.query.MongoQueryExecution$GeoNearExecution.execute(MongoQueryExecution.java:149) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.doExecute(AbstractMongoQuery.java:126) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:101) ~[spring-data-mongodb-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:618) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at com.sun.proxy.$Proxy85.findByAddressLocationWithin(Unknown Source) ~[na:na]
at com.cjava.expert.controller.CommerceController.findByAddressLocationWithIn(CommerceController.java:151) ~[classes/:na]
实体为:
@Getter @Setter
public class Address {
private @NonNull String address;
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2D)
private @NonNull Double[] location;
@Transient
private @Null Distance distance;
public Address(String address, Double[] location) {
this.address = address;
this.location = location;
}
}
存储库为:
public interface CommerceRepository extends MongoRepository<Commerce, String> {
GeoResults<Commerce> findByAddressLocationNear(Point point, Distance distance);
GeoResults<Commerce> findByAddressLocationWithin(Box box);
}
使用findByAddressLocationNear(Point point, Distance distance)
方法有效,但使用findByAddressLocationWithin(Box box)
方法无效。
控制器为:
@GetMapping("/commerces/near")
public List<Commerce> findByAddressLocationNear(@RequestParam(name="latLon") String latLon)
{
Double latitude = Double.parseDouble(latLon.split(",")[0].trim());
Double longitude = Double.parseDouble(latLon.split(",")[1].trim());
Point referenceLocation = new Point(latitude, longitude);
Distance tenMeters = new Distance(0.1, Metrics.KILOMETERS);
List<Commerce> commerces = new ArrayList<Commerce>();
GeoResults<Commerce> result = commerceRepository.findByAddressLocationNear(referenceLocation, tenMeters);
for(int i = 0; i < result.getContent().size(); i++)
{
Commerce comm = result.getContent().get(i).getContent();
comm.getAddress().setDistance(result.getContent().get(i).getDistance());
commerces.add(comm);
}
return commerces;
}
@GetMapping("/commerces/near2")
public List<Commerce> findByAddressLocationWithIn(@RequestParam(name="latLon") String latLon)
{
Double meters = 10.00;
Double latitude = Double.parseDouble(latLon.split(",")[0].trim());
Double longitude = Double.parseDouble(latLon.split(",")[1].trim());
Point point1 = new GeoJsonPoint(latitude-(meters/2), longitude-(meters/2));
Point point2 = new GeoJsonPoint(latitude+(meters/2), longitude+(meters/2));
Box square = new Box(point1, point2);
List<Commerce> commerces = new ArrayList<Commerce>();
GeoResults<Commerce> result = commerceRepository.findByAddressLocationWithin(square);
for(int i = 0; i < result.getContent().size(); i++)
{
Commerce comm = result.getContent().get(i).getContent();
comm.getAddress().setDistance(result.getContent().get(i).getDistance());
commerces.add(comm);
}
return commerces;
}
希望有人可以帮助我。我已经遇到这个问题3天了,但我解决不了。对不起,我的英语不好。我是mongodb的Spring Boot新手