Reputation: 69
I tried hard to build an example to save my enity like below:
Resource class has ResourceDetail in which I defined 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;
}
I have added following mapper also in 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) {
}
}
}
I am getting this error:
{
"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"
}
I used this format to save my enity with 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 }
}
Please help me to solve this. Thanks
Upvotes: 1
Views: 3524
Reputation: 562
Tested your Mixin code an it worked. Although, I would suggest you to make sure you are sending application/json
as content type and the json structure is correct (In your example there is a missing }
).
There is a similar question like yours and this issue can be solved by registering a GeoJsonModule as well: https://stackoverflow.com/a/37340077/3697851
Upvotes: 0
Reputation: 341
Probably late, but you need to use GeoJSON format in your mongo.
"location" : {
"type" : "Point",
"coordinates" : [
-2.6637,
54.6944
]
}
be aware that coordinates are in this order : longitude, latitude
See more : http://geojson.org/ http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/geo/GeoJson.html
Upvotes: 1