Skip to content

Commit 01533ca

Browse files
committed
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.
1 parent a1f6dc6 commit 01533ca

File tree

4 files changed

+112
-13
lines changed

4 files changed

+112
-13
lines changed

spring-data-mongodb/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@
165165
<version>${equalsverifier}</version>
166166
<scope>test</scope>
167167
</dependency>
168+
169+
<dependency>
170+
<groupId>org.springframework</groupId>
171+
<artifactId>spring-webmvc</artifactId>
172+
<scope>test</scope>
173+
</dependency>
174+
168175
</dependencies>
169176

170177
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core;
17+
18+
import org.springframework.context.annotation.Bean;
19+
import org.springframework.data.mongodb.core.geo.GeoJsonModule;
20+
import org.springframework.data.web.config.SpringDataWebConfigurationMixin;
21+
22+
/**
23+
* Configuration class to expose {@link GeoJsonModule} as a Spring bean.
24+
*
25+
* @author Oliver Gierke
26+
*/
27+
@SpringDataWebConfigurationMixin
28+
public class GeoJsonConfiguration {
29+
30+
@Bean
31+
public GeoJsonModule geoJsonModule() {
32+
return new GeoJsonModule();
33+
}
34+
}

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

+18-13
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,30 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import org.springframework.data.geo.GeoModule;
2423
import org.springframework.data.geo.Point;
2524

2625
import com.fasterxml.jackson.core.JsonParser;
2726
import com.fasterxml.jackson.core.JsonProcessingException;
2827
import com.fasterxml.jackson.databind.DeserializationContext;
2928
import com.fasterxml.jackson.databind.JsonDeserializer;
3029
import com.fasterxml.jackson.databind.JsonNode;
30+
import com.fasterxml.jackson.databind.JsonSerializer;
31+
import com.fasterxml.jackson.databind.Module;
32+
import com.fasterxml.jackson.databind.module.SimpleModule;
3133
import com.fasterxml.jackson.databind.node.ArrayNode;
3234

3335
/**
36+
* A Jackson {@link Module} to register custom {@link JsonSerializer} and {@link JsonDeserializer}s for GeoJSON types.
37+
*
3438
* @author Christoph Strobl
39+
* @author Oliver Gierke
3540
* @since 1.7
3641
*/
37-
public class GeoJsonModule extends GeoModule {
42+
public class GeoJsonModule extends SimpleModule {
43+
44+
private static final long serialVersionUID = -8723016728655643720L;
3845

3946
public GeoJsonModule() {
40-
super();
4147

4248
addDeserializer(GeoJsonPoint.class, new GeoJsonPointDeserializer());
4349
addDeserializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointDeserializer());
@@ -51,7 +57,7 @@ public GeoJsonModule() {
5157
* @author Christoph Strobl
5258
* @since 1.7
5359
*/
54-
static abstract class GeoJsonDeserializer<T extends GeoJson<?>> extends JsonDeserializer<T> {
60+
private static abstract class GeoJsonDeserializer<T extends GeoJson<?>> extends JsonDeserializer<T> {
5561

5662
/*
5763
* (non-Javadoc)
@@ -148,7 +154,7 @@ protected GeoJsonLineString toLineString(ArrayNode node) {
148154
* @author Christoph Strobl
149155
* @since 1.7
150156
*/
151-
static class GeoJsonPointDeserializer extends GeoJsonDeserializer<GeoJsonPoint> {
157+
private static class GeoJsonPointDeserializer extends GeoJsonDeserializer<GeoJsonPoint> {
152158

153159
/*
154160
* (non-Javadoc)
@@ -177,7 +183,7 @@ protected GeoJsonPoint doDeserialize(ArrayNode coordinates) {
177183
* @author Christoph Strobl
178184
* @since 1.7
179185
*/
180-
static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer<GeoJsonLineString> {
186+
private static class GeoJsonLineStringDeserializer extends GeoJsonDeserializer<GeoJsonLineString> {
181187

182188
/*
183189
* (non-Javadoc)
@@ -206,7 +212,7 @@ protected GeoJsonLineString doDeserialize(ArrayNode coordinates) {
206212
* @author Christoph Strobl
207213
* @since 1.7
208214
*/
209-
static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer<GeoJsonMultiPoint> {
215+
private static class GeoJsonMultiPointDeserializer extends GeoJsonDeserializer<GeoJsonMultiPoint> {
210216

211217
/*
212218
* (non-Javadoc)
@@ -236,7 +242,7 @@ protected GeoJsonMultiPoint doDeserialize(ArrayNode coordinates) {
236242
* @author Christoph Strobl
237243
* @since 1.7
238244
*/
239-
static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer<GeoJsonMultiLineString> {
245+
private static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeserializer<GeoJsonMultiLineString> {
240246

241247
/*
242248
* (non-Javadoc)
@@ -248,7 +254,6 @@ protected GeoJsonMultiLineString doDeserialize(ArrayNode coordinates) {
248254
List<GeoJsonLineString> lines = new ArrayList<GeoJsonLineString>(coordinates.size());
249255

250256
for (JsonNode lineString : coordinates) {
251-
252257
if (lineString.isArray()) {
253258
lines.add(toLineString((ArrayNode) lineString));
254259
}
@@ -275,7 +280,7 @@ protected GeoJsonMultiLineString doDeserialize(ArrayNode coordinates) {
275280
* @author Christoph Strobl
276281
* @since 1.7
277282
*/
278-
static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer<GeoJsonPolygon> {
283+
private static class GeoJsonPolygonDeserializer extends GeoJsonDeserializer<GeoJsonPolygon> {
279284

280285
/*
281286
* (non-Javadoc)
@@ -287,9 +292,9 @@ protected GeoJsonPolygon doDeserialize(ArrayNode coordinates) {
287292
for (JsonNode ring : coordinates) {
288293

289294
// currently we do not support holes in polygons.
290-
GeoJsonPolygon polygon = new GeoJsonPolygon(toPoints((ArrayNode) ring));
291-
return polygon;
295+
return new GeoJsonPolygon(toPoints((ArrayNode) ring));
292296
}
297+
293298
return null;
294299
}
295300
}
@@ -313,7 +318,7 @@ protected GeoJsonPolygon doDeserialize(ArrayNode coordinates) {
313318
* @author Christoph Strobl
314319
* @since 1.7
315320
*/
316-
static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer<GeoJsonMultiPolygon> {
321+
private static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer<GeoJsonMultiPolygon> {
317322

318323
/*
319324
* (non-Javadoc)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.config;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.data.mongodb.core.GeoJsonConfiguration;
26+
import org.springframework.data.mongodb.core.geo.GeoJsonModule;
27+
import org.springframework.data.web.config.EnableSpringDataWebSupport;
28+
import org.springframework.test.context.ContextConfiguration;
29+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30+
31+
/**
32+
* Integration tests for {@link GeoJsonConfiguration}.
33+
*
34+
* @author Oliver Gierke
35+
*/
36+
@RunWith(SpringJUnit4ClassRunner.class)
37+
@ContextConfiguration
38+
public class GeoJsonConfigurationIntegrationTests {
39+
40+
@Configuration
41+
@EnableSpringDataWebSupport
42+
static class Config {}
43+
44+
@Autowired GeoJsonModule geoJsonModule;
45+
46+
/**
47+
* @see DATAMONGO-1181
48+
*/
49+
@Test
50+
public void picksUpGeoJsonModuleConfigurationByDefault() {
51+
assertThat(geoJsonModule, is(notNullValue()));
52+
}
53+
}

0 commit comments

Comments
 (0)