使用GeoJsonPoint与mongoDB和spring数据

时间:2015-10-26 11:34:11

标签: mongodb spring-data geojson spring-data-mongodb

我努力建立一个例子来保存我的恩赐,如下所示:

资源类有ResourceDetail,我在其中定义了GeoJsonPoint。

@Document(collection = "Resource")
public class Resource implements Serializable {

    /**
     * 
     */
    private static final Long serialVersionUID = 1L;

    @Id
    private Long id;
    private String name;
    private Integer age;
    private String orgName;
    private String resourceType;
    private String department;
    private boolean showPrice;
    private Integer price;
    private ResourceDetail resourceDetail;
}



@Document(collection = "ResourceDetail")
public class ResourceDetail implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String identityNumber;
    @DateTimeFormat(iso = ISO.DATE)
    private Date birthDate;
    private String addressLine1;
    private String addressLine2;
    private String specialization;
    private GeoJsonPoint location;
}

我在Appconfig中添加了以下映射器:

@Configuration
@ComponentScan(basePackages = "com.test")
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(new MongoClient(), "test");
    }
    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
        return mongoTemplate;
    }
    /**
     * Read JSON data from disk and insert those stores.
     * 
     * @return
     */
    public @Bean ObjectMapper repositoryPopulator() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixIn(GeoJsonPoint.class, GeoJsonPointMixin.class);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return mapper;
    }
    static abstract class GeoJsonPointMixin {
        GeoJsonPointMixin(@JsonProperty("longitude") double x, @JsonProperty("latitude") double y) {
        }
    }
}

我收到此错误:

{
    "timestamp": 1445857673601,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "Could not read JSON: No suitable constructor found for type [simple type, class org.springframework.data.mongodb.core.geo.GeoJsonPoint]: can not instantiate from JSON object (need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@d7eff8; line: 13, column: 25] (through reference chain: com.appointment.domain.Resource[\"resourceDetail\"]->com.appointment.domain.ResourceDetail[\"location\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.springframework.data.mongodb.core.geo.GeoJsonPoint]: can not instantiate from JSON object (need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@d7eff8; line: 13, column: 25] (through reference chain: com.appointment.domain.Resource[\"resourceDetail\"]->com.appointment.domain.ResourceDetail[\"location\"])",
    "path": "/rest/resource/add"
}

我使用这种格式来保存我的GeoJsonPoint:

{

  "name":"Test",
  "age": 32,
  "orgName":"testOrg",
  "resourceType":"testresourcType",
  "price":1200,
  "department":"testDepartment",
  "resourceDetail": {
                    "identityNumber": "3",
                    "birthDate": "2000-10-10",
    "location" : { "latitude":40.743827, "longitude":-73.989015 }

}

请帮我解决这个问题。感谢

2 个答案:

答案 0 :(得分:1)

可能晚了,但你需要在你的mongo中使用GeoJSON格式。

"location" : {
        "type" : "Point", 
        "coordinates" : [
            -2.6637, 
            54.6944
        ]
}

请注意坐标的顺序如下:经度,纬度

查看更多: http://geojson.org/ http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/geo/GeoJson.html

答案 1 :(得分:0)

测试你的Mixin代码是否有效。虽然,我建议您确保发送application/json作为内容类型,并且json结构正确(在您的示例中,缺少})。

有一个类似于你的问题,这个问题也可以通过注册GeoJsonModule来解决:https://stackoverflow.com/a/37340077/3697851