Browse Source

DATAMONGO-1181 - Register GeoJsonModule with @EnableSpringDataWebSupport.

Added the necessary configuration infrastructure to automatically register the GeoJsonModule as Spring bean when @EnableSpringDataWebSupport is used. This is implemented by exposing a configuration class annotated with @SpringDataWebConfigurationMixin.

Added Spring WebMVC as test dependency to be able to write an integration test. Polished GeoJsonModule to hide the actual serializers.

Original pull request: #283.
Related ticket: DATACMNS-660.
pull/282/merge
Oliver Gierke 11 years ago
parent
commit
01533ca34c
  1. 7
      spring-data-mongodb/pom.xml
  2. 34
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java
  3. 31
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java
  4. 53
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java

7
spring-data-mongodb/pom.xml

@ -165,6 +165,13 @@ @@ -165,6 +165,13 @@
<version>${equalsverifier}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

34
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/GeoJsonConfiguration.java

@ -0,0 +1,34 @@ @@ -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();
}
}

31
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java

@ -20,7 +20,6 @@ import java.util.ArrayList; @@ -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; @@ -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 { @@ -51,7 +57,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static abstract class GeoJsonDeserializer<T extends GeoJson<?>> extends JsonDeserializer<T> {
private static abstract class GeoJsonDeserializer<T extends GeoJson<?>> extends JsonDeserializer<T> {
/*
* (non-Javadoc)
@ -148,7 +154,7 @@ public class GeoJsonModule extends GeoModule { @@ -148,7 +154,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static class GeoJsonPointDeserializer extends GeoJsonDeserializer<GeoJsonPoint> {
private static class GeoJsonPointDeserializer extends GeoJsonDeserializer<GeoJsonPoint> {
/*
* (non-Javadoc)
@ -177,7 +183,7 @@ public class GeoJsonModule extends GeoModule { @@ -177,7 +183,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer<GeoJsonLineString> {
private static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer<GeoJsonLineString> {
/*
* (non-Javadoc)
@ -206,7 +212,7 @@ public class GeoJsonModule extends GeoModule { @@ -206,7 +212,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer<GeoJsonMultiPoint> {
private static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer<GeoJsonMultiPoint> {
/*
* (non-Javadoc)
@ -236,7 +242,7 @@ public class GeoJsonModule extends GeoModule { @@ -236,7 +242,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer<GeoJsonMultiLineString> {
private static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer<GeoJsonMultiLineString> {
/*
* (non-Javadoc)
@ -248,7 +254,6 @@ public class GeoJsonModule extends GeoModule { @@ -248,7 +254,6 @@ public class GeoJsonModule extends GeoModule {
List<GeoJsonLineString> lines = new ArrayList<GeoJsonLineString>(coordinates.size());
for (JsonNode lineString : coordinates) {
if (lineString.isArray()) {
lines.add(toLineString((ArrayNode) lineString));
}
@ -275,7 +280,7 @@ public class GeoJsonModule extends GeoModule { @@ -275,7 +280,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer<GeoJsonPolygon> {
private static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer<GeoJsonPolygon> {
/*
* (non-Javadoc)
@ -287,9 +292,9 @@ public class GeoJsonModule extends GeoModule { @@ -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 { @@ -313,7 +318,7 @@ public class GeoJsonModule extends GeoModule {
* @author Christoph Strobl
* @since 1.7
*/
static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer<GeoJsonMultiPolygon> {
private static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer<GeoJsonMultiPolygon> {
/*
* (non-Javadoc)

53
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/GeoJsonConfigurationIntegrationTests.java

@ -0,0 +1,53 @@ @@ -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()));
}
}
Loading…
Cancel
Save