MongoDB自定义转换



在我的Spring Boot应用程序中,我正在尝试从MongoDB数据库中检索位于某个区域的商店列表。这些商店以这种形式在数据库中:

{
   _id:ObjectId("5a0c6711fb3aac66aafe26c8")
   picture:"http://placehold.it/150x150"
   name:"Sonique"
   email:"leilaware@sonique.com"
   city:"Rabat"
   location:Object
      type:"Point"
      coordinates:Array
          0:-6.74938
          1:33.83436
}

正在检索商店的商店企业类是:

package com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.tech_services.persistence;
import com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.domain.model.Location;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "shops")
public class ShopEntity {
    @Id
    private ObjectId id;
    private String picture;
    private String name;
    private String email;
    private String city;
    private Location location;
    public ShopEntity() {}
    public ShopEntity(ObjectId id, String picture, String name, String email, String city, Location location) {
        this.id = id;
        this.picture = picture;
        this.name = name;
        this.email = email;
        this.city = city;
        this.location = location;
    }
    public ObjectId getId() {
        return id;
    }
    public void setId(ObjectId id) {
        this.id = id;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public Location getLocation() {
        return location;
    }
    public void setLocation(Location location) {
        this.location = location;
    }
    @Override
    public String toString() {
        return "Shop{n" +
                "tid: " + id + 'n' +
                "tpicture: " + picture + 'n' +
                "tname: " + name + 'n' +
                "temail: " + email + 'n' +
                "tcity: " + city + 'n' +
                "tlocation: " + location + 'n' +
                '}';
    }
}

位置类是一个简单的pojo:

package com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.domain.model;
public class Location {
    private double latitude;
    private double longitude;
    public Location() {}
    public Location(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }
    public double getLatitude() {
        return latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    @Override
    public String toString() {
        return "{ latitude: " + latitude + ", " +
                "longitude: " + longitude + " " +
                "}";
    }
}

为了将geojsonpoint位置属性转换为我的自定义位置对象,我制作了一个自定义转换器来处理这种情况:

package com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.tech_services.persistence;
import com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.domain.model.Location;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
@ReadingConverter
public class GeoJsonPointToLocationConverter implements Converter<GeoJsonPoint, Location> {
    @Override
    public Location convert(GeoJsonPoint source) {
        Location location = new Location(source.getY(), source.getX());
        return location;
    }
}

然后,我已经将此转换器注册到mongo配置中:

package com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.tech_services.persistence;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class MongoConfig extends AbstractMongoConfiguration {
    @Override
    protected String getDatabaseName() {
        return "shop_finder";
    }
    @Override
    public Mongo mongo() {
        return new MongoClient("127.0.0.1", 27017);
    }
    @Bean
    @Override
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new GeoJsonPointToLocationConverter());
        return new CustomConversions(converters);
    }
}

不幸的是,这是不起作用的,正在检索商店,但是位置属性不会使用适当的坐标初始化:

Shop{
    id: 5a0c6b42fd3eb67969316d83
    picture: http://placehold.it/150x150
    name: Sonique
    email: leilaware@sonique.com
    city: Rabat
    location: { latitude: 0.0, longitude: 0.0 }
}

请,我在做什么错!

编辑:为了将持久性层与其他图层解矛,我只是通过立面将实体作为域类暴露为域类,如下所示:

package com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.tech_services.persistence;
import com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.domain.model.Location;
import com.hidden_founders.jobs.software_engineer_java.coding_challenge.shopfinder.domain.model.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@EnableMongoRepositories
@Service
public class MongoFacade {
    @Autowired
    private ShopsRepository shopsRepository;
    public List<Shop> findShopsWithin(double centerLatitude, double centerLongitude, double radiusInKm) {
        List<Shop> shops = new ArrayList<>();
        List<ShopEntity> shopEntities = shopsRepository.findByLocationWithin(new Circle(centerLongitude, centerLatitude, radiusInKm/111.12));
        for (ShopEntity shopEntity :
                shopEntities) {
            shops.add(new Shop(shopEntity.getPicture(), shopEntity.getName(), shopEntity.getEmail(), shopEntity.getCity(),
                    new Location(shopEntity.getLocation().getY(), shopEntity.getLocation().getX())));
        }
        return shops;
    }
}

您的自定义转换之所以称为,是因为您在转换类中提到的类型(GeoJsonPoint(未在您的实体(Shopentity(类中使用。

mongo只有在必须将数据库中的对象转换为已指定的实体类型时,并且在该过程中,如果它在转换类中指定了您已指定的类型,则会调用您的转换器(ReadingConverter(。

相关内容

  • 没有找到相关文章

最新更新