I have a @Entity
model that has a property of type com.vividsolutions.jts.geom.Point
. When I try to render this model in a @RestController
I get a recursion exception.
(StackOverflowError); nested exception is
com.fasterxml.jackson.databind.JsonMappingException: Infinite
recursion (StackOverflowError) (through reference chain:
com.vividsolutions.jts.geom.Point["envelope"]-
>com.vividsolutions.jts.geom.Point["envelope"]....
The entity looks like this (shortened for brevity):
@Entity
@Data
public class MyEntity{
// ...
@Column(columnDefinition = "geometry")
private Point location;
// ...
}
After some research I found out that this is because Jackson cannot deserialize GeoJson by default. Adding this library should solve the issue: https://github.com/bedatadriven/jackson-datatype-jts.
I am now not sure how to include this module in the object mapper in spring boot. As per documentation in boot, I tried adding it to the @Configuration
in the following two ways:
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.modulesToInstall(new JtsModule());
return builder;
}
and
@Bean
public JtsModule jtsModule(){
return new JtsModule();
}
Both didn't remove the exception. Sry if this is a duplicate, but all I was able to find SO were customising the ObjectMapper
which in my understanding of the documentation is no the "spring boot way".
As a workaround I am @JsonIgnore
ing the Point
and have custom getters and setters for a non existent coordinated object,... but it's not the way I'd like to keep it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…