Ramblings of a Java developer and OSS fanatic

Using GeoJSON With Spring Data for MongoDB and Spring Boot

In my previous articles I compared 4 frameworks commonly used in communicating with MongoDB from the JVM and found out that in that use-case, Spring Data for MongoDB was the easiest solution. However I did make the remark that it doesn’t use the GeoJSON format to store geolocation coordinates and geometries.

I tried to add GeoJSON support before, but couldn’t get the conversion to work propertly. But after some extensive searching I found out that the reason for it not working was my use of Spring Boot: its autoconfiguration for MongoDB does not support custom conversion out of the box.

A Simple Use-case Comparison of JVM Libraries for MongoDB

MongoDB is one of my favorite data stores when it comes to storing document-based JSON data. Communicating with MongoDB with JVM languages can be done in a lot of ways. I thought it would be a nice exercise to take 4 of the most popular solutions and implement a simple use case in each of those solutions. The use case: create a REST service that can get a list of cities and get the nearest city for a given city with the distance to that city.

The 4 approaches I’ll compare are using the standard MongoDB Java Driver, Jongo, Morphia and finally Spring Data for MongoDB. All the code is written in Groovy for brevity and I’ll be using Spring Boot to minimize boilerplate code to provide the REST layer.

Using Animal Sniffer With Gradle

When compiling code in Gradle that targets older JVM’s, you need to be very careful your code doesn’t use any of the classes in the newer JDK. For example, it’s very easy to create a class that uses the new DateTime API in Java 8 and compile it to target Java 6. The compilation will succeed but the class won’t run on a Java 6 JVM. But the changes can be harder to spot. For example, the Collection API has changed a bit in Java 8 and some classes have added methods. The result would be the same.