In my PostgreSQL 9.3 + PostGIS 2.1.5 I have a table PLACE
with a column coordinates
of type Geometry(Point,26910)
.
I want to map it to Place
entity in my Spring Boot 1.1.9 web application, which uses Hibernate 4.0.0 + . Place
is available with a REST repository.
Unfortunately when I GET http://localhost:8080/mywebapp/places
I receive this strange JSON response:
{
"_embedded" : {
"venues" : [ {
"id" : 1,
"coordinates" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
"envelope" : {
and so on indefinetely...! Spring log doesn't help..
I'm working with this application.properties:
spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:postgresql://192.168.1.123/mywebapp
spring.datasource.username=postgres
spring.datasource.password=mypwd
spring.datasource.driverClassName=org.postgresql.Driver
First of all, is it ok to use database-platform
instead of database
?
And maybe do I have to use following settings instead of the above?
spring.datasource.url=jdbc:postgresql_postGIS://192.168.1.123/mywebapp
spring.datasource.driverClassName=org.postgis.DriverWrapper
Anyway my entity is something like this:
@Entity
public class Place {
@Id
public int id;
@Column(columnDefinition="Geometry")
@Type(type="org.hibernate.spatial.GeometryType") //"org.hibernatespatial.GeometryUserType" seems to be for older versions of Hibernate Spatial
public com.vividsolutions.jts.geom.Point coordinates;
}
My pom.xml contains this relevant part:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1102-jdbc41</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>4.3</version><!-- compatible with Hibernate 4.3.x -->
<exclusions>
<exclusion>
<artifactId>postgresql</artifactId>
<groupId>postgresql</groupId>
</exclusion>
</exclusions>
</dependency>
A bit strange configuration, I found it on the internet, it is the one that works best for now.
I hope that someone could help me with this mistery. :)
The problem doesn't appear to be related to PostgreSQL. It appears that your POJO has a backreference, which means that your mapper doesn't know how to handle it. You need to explicitly define the recursive relationships so that the mapper knows when to stop. (My Goto link --> http://vard-lokkur.blogspot.com/2010/10/json-jackson-to-rescue.html)
This serialization/deserialization also worked fine for me.
https://github.com/bedatadriven/jackson-datatype-jts
Finally I discovered that my configuration is ok and might be Jackson that cannot manage
Point
data type correctly. So I customized its JSON serialization and deserialization:add these annotations to our
coordinates
field:create such serializer:
create such deserializer:
Maybe you can also use this serializer and this deserializer, available here.
The solutions above helped me to fix the problem. I simplify it so other people can understand.
I included this library in my pom.xml:
This is the POJO object I used. Then I was able to get the REST call to work without the envelope error and proper coodinates.
My table had these 2 columns: