diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index d0f386e7e..312712720 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -165,6 +165,13 @@ ${equalsverifier} test + + + org.springframework + spring-webmvc + test + + diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java new file mode 100644 index 000000000..cb29dca57 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java @@ -0,0 +1,34 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.core; + +import org.springframework.context.annotation.Bean; +import org.springframework.data.mongodb.core.geo.GeoJsonModule; +import org.springframework.data.web.config.SpringDataWebConfigurationMixin; + +/** + * Configuration class to expose {@link GeoJsonModule} as a Spring bean. + * + * @author Oliver Gierke + */ +@SpringDataWebConfigurationMixin +public class GeoJsonConfiguration { + + @Bean + public GeoJsonModule geoJsonModule() { + return new GeoJsonModule(); + } +} 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 e7e3879d4..6b8be6f0c 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 @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.springframework.data.geo.GeoModule; import org.springframework.data.geo.Point; import com.fasterxml.jackson.core.JsonParser; @@ -28,16 +27,23 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.node.ArrayNode; /** + * A Jackson {@link Module} to register custom {@link JsonSerializer} and {@link JsonDeserializer}s for GeoJSON types. + * * @author Christoph Strobl + * @author Oliver Gierke * @since 1.7 */ -public class GeoJsonModule extends GeoModule { +public class GeoJsonModule extends SimpleModule { + + private static final long serialVersionUID = -8723016728655643720L; public GeoJsonModule() { - super(); addDeserializer(GeoJsonPoint.class, new GeoJsonPointDeserializer()); addDeserializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointDeserializer()); @@ -51,7 +57,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static abstract class GeoJsonDeserializer> extends JsonDeserializer { + private static abstract class GeoJsonDeserializer> extends JsonDeserializer { /* * (non-Javadoc) @@ -148,7 +154,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static class GeoJsonPointDeserializer extends GeoJsonDeserializer { + private static class GeoJsonPointDeserializer extends GeoJsonDeserializer { /* * (non-Javadoc) @@ -177,7 +183,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer { + private static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer { /* * (non-Javadoc) @@ -206,7 +212,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer { + private static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer { /* * (non-Javadoc) @@ -236,7 +242,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer { + private static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer { /* * (non-Javadoc) @@ -248,7 +254,6 @@ public class GeoJsonModule extends GeoModule { List lines = new ArrayList(coordinates.size()); for (JsonNode lineString : coordinates) { - if (lineString.isArray()) { lines.add(toLineString((ArrayNode) lineString)); } @@ -275,7 +280,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer { + private static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer { /* * (non-Javadoc) @@ -287,9 +292,9 @@ public class GeoJsonModule extends GeoModule { for (JsonNode ring : coordinates) { // currently we do not support holes in polygons. - GeoJsonPolygon polygon = new GeoJsonPolygon(toPoints((ArrayNode) ring)); - return polygon; + return new GeoJsonPolygon(toPoints((ArrayNode) ring)); } + return null; } } @@ -313,7 +318,7 @@ public class GeoJsonModule extends GeoModule { * @author Christoph Strobl * @since 1.7 */ - static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer { + private static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer { /* * (non-Javadoc) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java new file mode 100644 index 000000000..7efa80677 --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.mongodb.config; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.GeoJsonConfiguration; +import org.springframework.data.mongodb.core.geo.GeoJsonModule; +import org.springframework.data.web.config.EnableSpringDataWebSupport; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Integration tests for {@link GeoJsonConfiguration}. + * + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class GeoJsonConfigurationIntegrationTests { + + @Configuration + @EnableSpringDataWebSupport + static class Config {} + + @Autowired GeoJsonModule geoJsonModule; + + /** + * @see DATAMONGO-1181 + */ + @Test + public void picksUpGeoJsonModuleConfigurationByDefault() { + assertThat(geoJsonModule, is(notNullValue())); + } +}