2016-10-31 7 views
0

私はこれまでSpringブートを使用したことがなく、mongodbを使用して小さなRESTサービスを作成したいと考えています。私はいくつかの空間データをデータベースに保存していて、残りのインターフェースからアクセスできるようにしたいと考えています。これはうまくいきましたが、私はデフォルトのmappig/marshallingを使ってGeojsonの表現に苦しんでいます。だからここSpringブートでmongodbからgeojson(GeoJsonPolygon)のマーシャリングを変更するにはどうすればいいですか?

は、私がこれまで持っているものです。

POJO:

{ "_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コントローラのメソッド::MongoDBの中

@Document(collection = "geofences") 
public class Geofence { 
    @Id 
    private String id; 
    private String topic; 
    private Date expiresOn; 
    private GeoJsonPolygon geo; 

    // getters and setters 
} 

ドキュメント

public ResponseEntity<Collection<Geofence>> getAll() { 
    return new ResponseEntity<>(repository.findAll(), HttpStatus.OK); 
} 

ことで休憩サービスIを呼び出すそれは私の文書にあるように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" 
    } 
    } 
] 

任意の提案ですか?この動作を変更するにはどうすればよいですか?

答えて

3

私は、カスタム・シリアライザ

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

を書いたが、私は知らないアウトオブボックスソリューションが存在しなければなりませんか?

+0

私はまったく同じ問題に遭遇しましたが、これは本当にばかばかしいようです。シリアライザのおかげで、「ソリューション」を投稿するのに大変です! – gabowsky

+0

聞いてうれしい、私はこの正確な理由でそれをやった:) – greyno

関連する問題