如何在 Spring Boot 中从 mongodb 更改 geojson (GeoJsonPolygon) 的编组?

Posted

技术标签:

【中文标题】如何在 Spring Boot 中从 mongodb 更改 geojson (GeoJsonPolygon) 的编组?【英文标题】:How to change marshalling of geojson (GeoJsonPolygon) from mongodb in Spring Boot? 【发布时间】:2017-03-13 04:15:43 【问题描述】:

我以前从未使用过 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"
    
  
]

有什么建议吗?我该如何改变这种行为?

【问题讨论】:

【参考方案1】:

我写了一个自定义序列化器

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();
    

但必须有一个我不知道的开箱即用解决方案?

【讨论】:

我刚刚遇到了同样的问题,这看起来确实很荒谬.. 感谢序列化程序,非常感谢发布“解决方案”! 很高兴听到,我这样做正是出于这个原因:) 这个 18' 仍然没有开箱即用的解决方案:/

以上是关于如何在 Spring Boot 中从 mongodb 更改 geojson (GeoJsonPolygon) 的编组?的主要内容,如果未能解决你的问题,请参考以下文章