So, if you look at all the classes from org.springframework.data.geo , you will notice that almost all classes do not have a no-arg constructor, which by default for ObjectMapper should deserialize the POJO from JSON.
One way around this with third-party APIs is to use Jackson Mixins . If you look at GeoModule , this is a module that you can register with ObjectMapper , which includes some Mixins
mapper.registerModule(new GeoModule());
If you look at org.springframework.data.mongodb.core.geo , you get another GeoJsonModule module, which also has Mixins. This module should take care of GeoJsonPoint .
But the main problem for your use case is (if you look at the GeoModule ), then there is no Mixin for GeoResult or GeoResults that you need to parse JSON on.
I created a module for servicing GeoResult , but GeoResults not working at the moment.
public class GeoModuleExt extends SimpleModule { public GeoModuleExt() { super("Mixins", new Version(1, 0, 0, null)); setMixInAnnotation(GeoResult.class, GeoResultMixin.class); setMixInAnnotation(GeoResults.class, GeoResultsMixin.class); } static abstract class GeoResultMixin { GeoResultMixin(@JsonProperty("content") Object content, @JsonProperty("distance") Distance distance) { } } static abstract class GeoResultsMixin { GeoResultsMixin(@JsonProperty("results")List<GeoResult> results) { } } }
You can play with him. I donβt have time to work on it (hence the half - @ $$ solution), but when I get some time, if you do not understand this, I will see what I can do.
As a test, you can use ObjectMapper offline to make sure that it works first
public class Test { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new GeoJsonModule()); mapper.registerModule(new GeoModule());
When you get a test for work, you can register ObjectMapper with Jersey as
ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new GeoJsonModule()); mapper.registerModule(new GeoModule()); // Our custom module mapper.registerModule(new GeoModuleExt()); ClientConfig config = new DefaultClientConfig(); config.getSingletons().add(new JacksonJsonProvider(mapper)); Client client = Client.create(config);
UPDATE
So, after some games, I was able to get it to work with this Mixin for GeoResults . Just upgrade above GeoModuleExt
static abstract class GeoResultsMixin { GeoResultsMixin(@JsonProperty("results") List<GeoResult> results, @JsonProperty("averageDistance") Distance averageDistance) { } @JsonProperty("results") abstract List<GeoResult> getContent(); }
It works, as expected, using the above test. Not tested with Jersey yet, but if it works with ObjectMapper , this should not be a problem with Jersey if we set up the Jackson provider to use the mapper.