我正在使用MongoDB Near查找最近的点,getMills是基于LatLng查找最近的点的方法。在获取数据和映射之前,它运行良好。映射时返回以下错误。 我的问题是我如何将类序列化为GeoJsonPoint?
返回此错误:无法序列化org.springframework.data.mongodb.core.geo.GeoJsonPoint类。
private List<Mill> getMills(GetNearByMillsRequest getNearByMillsRequest, BasicResponse basicResponse) {
// get farmerPosition
LatLng fieldPosition = getNearByMillsRequest.getFieldPosition();
double Longitude = fieldPosition.getLongitude();
double Latitude = fieldPosition.getLatitude();
System.out.println("Longitude : " + Longitude + "Latitude : "+ Latitude);
// creating GeoJson Object for farmer field location...
Point point = new Point(Latitude,Longitude);
GeoJsonPoint farmerFieldPosition = new GeoJsonPoint(point);
Float distance = getNearByMillsRequest.getDistance();
// find crop by crop Name
Query query = new Query();
query.addCriteria(Criteria.where(Mill.Constants.MILL_CROP_REQUIRED).elemMatch(Criteria.where(MillCropRequired.Constants.CROP).is(getNearByMillsRequest.getCrop())));
query.addCriteria(Criteria.where(Mill.Constants.MILL_LOCATION).near(farmerFieldPosition));
// TODO Get radius from UserPreferences
NearQuery nearQuery = NearQuery.near(farmerFieldPosition).maxDistance(new Distance(distance, Metrics.KILOMETERS));
nearQuery.query(query);
nearQuery.spherical(true); // if using 2dsphere index, otherwise delete or set false
nearQuery.num(10);
// Problem occurs here, in mapping part...
GeoResults<Mill> results = millDAO.getMongoOperations().geoNear(nearQuery, Mill.class);
System.out.println("results : " + results.getContent().size());
if (results.getContent().isEmpty()) {
basicResponse.setErrorCode(ErrorCode.FORBIDDEN_ERROR);
basicResponse.setResponse("No Crop Found for this region.");
return null;
}
List<GeoResult<Mill>> geoResults = results.getContent();
// add nearBy Mills and return this list...
List<Mill> nearByMills = new ArrayList<>();
for (GeoResult<Mill> result : geoResults) {
Mill mill = result.getContent();
nearByMills.add(mill);
}
return nearByMills;
}
这是我的工厂实体。
public class Mill extends AbstractEntity {
private String vendorId;
private String name;
private String emailAddress;
private String countryCode;
private String phoneNumber;
private String postalAddress;
@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
private GeoJsonPoint millLocation;
private String ginningCapacity;
private String storageCapacity;
private ArrayList<MillCropRequired> millCropRequired;
public String getVendorId() {
return vendorId;
}
public void setVendorId(String vendorId) {
this.vendorId = vendorId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPostalAddress() {
return postalAddress;
}
public void setPostalAddress(String postalAddress) {
this.postalAddress = postalAddress;
}
public GeoJsonPoint getMillLocation() {
return millLocation;
}
public void setMillLocation(GeoJsonPoint millLocation) {
this.millLocation = millLocation;
}
public String getGinningCapacity() {
return ginningCapacity;
}
public void setGinningCapacity(String ginningCapacity) {
this.ginningCapacity = ginningCapacity;
}
public ArrayList<MillCropRequired> getCropRequirnment() {
return millCropRequired;
}
public void setCropRequirnment(ArrayList<MillCropRequired> millCropRequired) {
this.millCropRequired = millCropRequired;
}
public String getStorageCapacity() {
return storageCapacity;
}
public void setStorageCapacity(String storageCapacity) {
this.storageCapacity = storageCapacity;
}
public static class Constants extends AbstractEntity.Constants {
public static final String PHONE_NUMBER = "phoneNumber";
public static final String VENDOR_ID = "vendorId";
public static final String MILL_CROP_REQUIRED = "millCropRequired";
public static final String MILL_LOCATION = "millLocation";
}
}