From c1417c4e4ba7ec9f1b9360ae6270170db5afb7aa Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 2 Feb 2021 14:36:42 +0100 Subject: [PATCH] Polishing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move registerSerializersIn(…) entirely to GeoJsonSerializersModule. Tweak Javadoc and wording. Original pull request: #3539. Closes #3517 --- .../data/mongodb/core/geo/GeoJsonModule.java | 30 +++++-------------- .../core/geo/GeoJsonSerializersModule.java | 17 +++++++---- .../core/geo/GeoJsonSerializersUnitTests.java | 17 ++++++----- src/main/asciidoc/reference/mongodb.adoc | 8 +++-- 4 files changed, 34 insertions(+), 38 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java index 0a57247c9..6336ab81f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java @@ -21,12 +21,6 @@ import java.util.Collections; import java.util.List; import org.springframework.data.geo.Point; -import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonLineStringSerializer; -import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonMultiLineStringSerializer; -import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonMultiPointSerializer; -import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonMultiPolygonSerializer; -import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonPointSerializer; -import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonPolygonSerializer; import org.springframework.lang.Nullable; import com.fasterxml.jackson.core.JsonParser; @@ -70,7 +64,7 @@ public class GeoJsonModule extends SimpleModule { *
  • {@link GeoJsonPolygon}
  • *
  • {@link GeoJsonMultiPolygon}
  • * - * + * * @return a {@link Module} containing {@link JsonDeserializer deserializers} for {@link GeoJson} types. * @since 3.2 */ @@ -100,7 +94,7 @@ public class GeoJsonModule extends SimpleModule { SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Serializers", new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson")); - registerSerializersIn(module); + GeoJsonSerializersModule.registerSerializersIn(module); return module; } @@ -124,21 +118,11 @@ public class GeoJsonModule extends SimpleModule { SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson", new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson")); - registerSerializersIn(module); + GeoJsonSerializersModule.registerSerializersIn(module); registerDeserializersIn(module); return module; } - private static void registerSerializersIn(SimpleModule module) { - - module.addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer()); - module.addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer()); - module.addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer()); - module.addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer()); - module.addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer()); - module.addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer()); - } - private static void registerDeserializersIn(SimpleModule module) { module.addDeserializer(GeoJsonPoint.class, new GeoJsonPointDeserializer()); @@ -227,7 +211,7 @@ public class GeoJsonModule extends SimpleModule { return Collections.emptyList(); } - List points = new ArrayList(node.size()); + List points = new ArrayList<>(node.size()); for (JsonNode coordinatePair : node) { if (coordinatePair.isArray()) { @@ -238,7 +222,7 @@ public class GeoJsonModule extends SimpleModule { } protected GeoJsonLineString toLineString(ArrayNode node) { - return new GeoJsonLineString(toPoints((ArrayNode) node)); + return new GeoJsonLineString(toPoints(node)); } } @@ -352,7 +336,7 @@ public class GeoJsonModule extends SimpleModule { @Override protected GeoJsonMultiLineString doDeserialize(ArrayNode coordinates) { - List lines = new ArrayList(coordinates.size()); + List lines = new ArrayList<>(coordinates.size()); for (JsonNode lineString : coordinates) { if (lineString.isArray()) { @@ -429,7 +413,7 @@ public class GeoJsonModule extends SimpleModule { @Override protected GeoJsonMultiPolygon doDeserialize(ArrayNode coordinates) { - List polygones = new ArrayList(coordinates.size()); + List polygones = new ArrayList<>(coordinates.size()); for (JsonNode polygon : coordinates) { for (JsonNode ring : polygon) { diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersModule.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersModule.java index 3f2642997..bb27768be 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersModule.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersModule.java @@ -37,13 +37,18 @@ class GeoJsonSerializersModule extends SimpleModule { private static final long serialVersionUID = 1340494654898895610L; GeoJsonSerializersModule() { + registerSerializersIn(this); + } - addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer()); - addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer()); - addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer()); - addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer()); - addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer()); - addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer()); + + static void registerSerializersIn(SimpleModule module) { + + module.addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer()); + module.addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer()); + module.addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer()); + module.addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer()); + module.addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer()); + module.addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer()); } /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersUnitTests.java index 498193b80..1ea4b4635 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonSerializersUnitTests.java @@ -28,12 +28,14 @@ import org.springframework.data.geo.Point; import com.fasterxml.jackson.databind.ObjectMapper; /** + * Unit tests for {@link GeoJsonSerializersModule}. + * * @author Bjorn Harvold * @author Christoph Strobl */ class GeoJsonSerializersUnitTests { - ObjectMapper mapper; + private ObjectMapper mapper; @BeforeEach void beforeEach() { @@ -51,7 +53,7 @@ class GeoJsonSerializersUnitTests { } @Test // GH-3517 - public void shouldSerializeGeoJsonLineStringCorrectly() throws IOException { + void shouldSerializeGeoJsonLineStringCorrectly() throws IOException { GeoJsonLineString lineString = new GeoJsonLineString( Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60))); @@ -61,7 +63,7 @@ class GeoJsonSerializersUnitTests { } @Test // GH-3517 - public void shouldSerializeGeoJsonMultiPointCorrectly() throws IOException { + void shouldSerializeGeoJsonMultiPointCorrectly() throws IOException { GeoJsonMultiPoint multiPoint = new GeoJsonMultiPoint( Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60))); @@ -71,7 +73,7 @@ class GeoJsonSerializersUnitTests { } @Test // GH-3517 - public void shouldSerializeJsonMultiLineStringCorrectly() throws IOException { + void shouldSerializeJsonMultiLineStringCorrectly() throws IOException { GeoJsonMultiLineString multiLineString = new GeoJsonMultiLineString( Arrays.asList(new Point(10, 20), new Point(30, 40)), Arrays.asList(new Point(50, 60), new Point(70, 80))); @@ -81,17 +83,18 @@ class GeoJsonSerializersUnitTests { } @Test // GH-3517 - public void shouldSerializeGeoJsonPolygonCorrectly() throws IOException { + void shouldSerializeGeoJsonPolygonCorrectly() throws IOException { List points = Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0)); GeoJsonPolygon polygon = new GeoJsonPolygon(points); - assertThat(mapper.writeValueAsString(polygon)).isEqualTo("{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}"); + assertThat(mapper.writeValueAsString(polygon)).isEqualTo( + "{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}"); } @Test // GH-3517 - public void shouldSerializeGeoJsonMultiPolygonCorrectly() throws IOException { + void shouldSerializeGeoJsonMultiPolygonCorrectly() throws IOException { String json = "{\"type\":\"MultiPolygon\",\"coordinates\":[" + "[" + "[" + "[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]" + "]" + "]," + "[" + "[" diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index aca6fed42..a072b0b16 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -1446,6 +1446,7 @@ The geo-near operations return a `GeoResults` wrapper object that encapsulates ` MongoDB supports https://geojson.org/[GeoJSON] and simple (legacy) coordinate pairs for geospatial data. Those formats can both be used for storing as well as querying data. See the https://docs.mongodb.org/manual/core/2dsphere/#geospatial-indexes-store-geojson/[MongoDB manual on GeoJSON support] to learn about requirements and restrictions. +[[mongo.geo-json.domain.classes]] ==== GeoJSON Types in Domain Classes Usage of https://geojson.org/[GeoJSON] types in domain classes is straightforward. The `org.springframework.data.mongodb.core.geo` package contains types such as `GeoJsonPoint`, `GeoJsonPolygon`, and others. These types are extend the existing `org.springframework.data.geo` types. The following example uses a `GeoJsonPoint`: @@ -1469,6 +1470,7 @@ public class Store { ---- ==== +[[mongo.geo-json.query-methods]] ==== GeoJSON Types in Repository Query Methods Using GeoJSON types as repository query parameters forces usage of the `$geometry` operator when creating the query, as the following example shows: @@ -1529,6 +1531,7 @@ repo.findByLocationWithin( <4> <4> Use the legacy format `$polygon` operator. ==== +[[mongo.geo-json.metrics]] ==== Metrics and Distance calculation Then MongoDB `$geoNear` operator allows usage of a GeoJSON Point or legacy coordinate pairs. @@ -1700,9 +1703,10 @@ Returning the 3 Documents just like the GeoJSON variant: <4> Distance from center point in _Kilometers_ - take it times 1000 to match _Meters_ of the GeoJSON variant. ==== +[[mongo.geo-json.jackson-modules]] ==== GeoJSON Jackson Modules -By using the <>, Spring Data adds additional Jackson ``Modules``s to the `ObjectMapper` for de-/serializing common types used by the Spring Data domain. +By using the <>, Spring Data registers additional Jackson ``Modules``s to the `ObjectMapper` for de-/serializing common Spring Data domain types. Please refer to the <> section to learn more about the infrastructure setup of this feature. The MongoDB module additionally registers ``JsonDeserializer``s for the following GeoJSON types via its `GeoJsonConfiguration` exposing the `GeoJsonModule`. @@ -1734,7 +1738,7 @@ class GeoJsonConfiguration implements SpringDataJacksonModules { [WARNING] ==== -The next major version (`4.0`) will by default register both, ``JsonDeserializer``s and ``JsonSerializer``s for GeoJSON types. +The next major version (`4.0`) will register both, ``JsonDeserializer``s and ``JsonSerializer``s for GeoJSON types by default. ==== [[mongo.textsearch]]