如何在 Spring 引导中从 mongodb 更改 geojson (GeoJsonPolygon) 的编组?
How to change marshalling of geojson (GeoJsonPolygon) from mongodb in Spring Boot?
我以前从未使用过 Spring Boot,我想使用 mongodb 创建一个小型 REST 服务。我正在数据库中存储一些空间数据,并希望通过其余接口访问它们。这有效并且非常简单但是我正在努力使用默认的 mappig/marshalling.
获得的 Geojson 表示
所以这是我目前的情况:
POJO:
@Document(collection = "geofences")
public class Geofence {
@Id
private String id;
private String topic;
private Date expiresOn;
private GeoJsonPolygon geo;
// getters and setters
}
mongodb 中的文档:
{ "_id" : ObjectId("5816b03b71e2e892bd7846f3"), "topic" : "ok", "expiresOn" : ISODate("2017-01-01T00:00:00Z"), "geo" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] } }
REST 控制器中的方法:
public ResponseEntity<Collection<Geofence>> getAll() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
通过调用其余服务,我想像在我的文档中一样接收 geojson 部分,但我得到的是:
[
{
"id": "5816b03b71e2e892bd7846f3",
"topic": "ok",
"expiresOn": 1483228800000,
"geo": {
"points": [
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
},
{
"x": 3,
"y": 6,
"type": "Point",
"coordinates": [
3,
6
]
},
{
"x": 6,
"y": 1,
"type": "Point",
"coordinates": [
6,
1
]
},
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
}
],
"coordinates": [
{
"type": "LineString",
"coordinates": [
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
},
{
"x": 3,
"y": 6,
"type": "Point",
"coordinates": [
3,
6
]
},
{
"x": 6,
"y": 1,
"type": "Point",
"coordinates": [
6,
1
]
},
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
}
]
}
],
"type": "Polygon"
}
}
]
有什么建议吗?我怎样才能改变这种行为?
我写了一个自定义序列化程序
public static class GeoJsonPolygonSerializer extends JsonSerializer<GeoJsonPolygon> {
@Override
public void serialize(GeoJsonPolygon value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeStringField("type", value.getType());
gen.writeArrayFieldStart("coordinates");
for (GeoJsonLineString ls : value.getCoordinates()) {
gen.writeStartArray();
for (Point p : ls.getCoordinates()) {
gen.writeObject(new double[]{p.getX(), p.getY()});
}
gen.writeEndArray();
}
gen.writeEndArray();
gen.writeEndObject();
}
}
但必须有一个我不知道的开箱即用的解决方案?
我以前从未使用过 Spring Boot,我想使用 mongodb 创建一个小型 REST 服务。我正在数据库中存储一些空间数据,并希望通过其余接口访问它们。这有效并且非常简单但是我正在努力使用默认的 mappig/marshalling.
获得的 Geojson 表示所以这是我目前的情况:
POJO:
@Document(collection = "geofences")
public class Geofence {
@Id
private String id;
private String topic;
private Date expiresOn;
private GeoJsonPolygon geo;
// getters and setters
}
mongodb 中的文档:
{ "_id" : ObjectId("5816b03b71e2e892bd7846f3"), "topic" : "ok", "expiresOn" : ISODate("2017-01-01T00:00:00Z"), "geo" : { "type" : "Polygon", "coordinates" : [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] } }
REST 控制器中的方法:
public ResponseEntity<Collection<Geofence>> getAll() {
return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
}
通过调用其余服务,我想像在我的文档中一样接收 geojson 部分,但我得到的是:
[
{
"id": "5816b03b71e2e892bd7846f3",
"topic": "ok",
"expiresOn": 1483228800000,
"geo": {
"points": [
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
},
{
"x": 3,
"y": 6,
"type": "Point",
"coordinates": [
3,
6
]
},
{
"x": 6,
"y": 1,
"type": "Point",
"coordinates": [
6,
1
]
},
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
}
],
"coordinates": [
{
"type": "LineString",
"coordinates": [
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
},
{
"x": 3,
"y": 6,
"type": "Point",
"coordinates": [
3,
6
]
},
{
"x": 6,
"y": 1,
"type": "Point",
"coordinates": [
6,
1
]
},
{
"x": 0,
"y": 0,
"type": "Point",
"coordinates": [
0,
0
]
}
]
}
],
"type": "Polygon"
}
}
]
有什么建议吗?我怎样才能改变这种行为?
我写了一个自定义序列化程序
public static class GeoJsonPolygonSerializer extends JsonSerializer<GeoJsonPolygon> {
@Override
public void serialize(GeoJsonPolygon value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeStringField("type", value.getType());
gen.writeArrayFieldStart("coordinates");
for (GeoJsonLineString ls : value.getCoordinates()) {
gen.writeStartArray();
for (Point p : ls.getCoordinates()) {
gen.writeObject(new double[]{p.getX(), p.getY()});
}
gen.writeEndArray();
}
gen.writeEndArray();
gen.writeEndObject();
}
}
但必须有一个我不知道的开箱即用的解决方案?