在我的PostgreSQL 9.3+PostGIS 2.1.5中,我有一个表PLACE,其中coordinates列的类型为Geometry(Point,26910).

我想将它映射到Spring Boot 1.1.9 web应用程序中的Place个实体,该应用程序使用Hibernate 4.0.0+.Place可与REST存储库一起使用.

不幸的是,当我GET http://localhost:8080/mywebapp/places岁时,我收到了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" : {

等等…等等...!Spring log帮不上忙..

我正在处理这个应用程序.属性:

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

先用database-platform代替database-platform行吗?

spring.datasource.url=jdbc:postgresql_postGIS://192.168.1.123/mywebapp
spring.datasource.driverClassName=org.postgis.DriverWrapper

总之,我的实体是这样的:

@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;
}

我的pom.xml包含以下相关部分:

<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>

有点奇怪的配置,我在网上找到的,这是目前最有效的配置.

我希望有人能帮我解决这个问题.:)

推荐答案

最后我发现我的配置还可以,可能是Jackson,无法正确管理Point个数据类型.所以我定制了它的JSON序列化和反序列化:

  • 将这些注释添加到我们的coordinates字段:

    @JsonSerialize(using = PointToJsonSerializer.class)
    @JsonDeserialize(using = JsonToPointDeserializer.class)
    
  • 创建这样的序列化程序:

    import java.io.IOException;
    import com.fasterxml.jackson.core.JsonGenerator;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.JsonSerializer;
    import com.fasterxml.jackson.databind.SerializerProvider;
    import com.vividsolutions.jts.geom.Point;
    
    public class PointToJsonSerializer extends JsonSerializer<Point> {
    
        @Override
        public void serialize(Point value, JsonGenerator jgen,
                SerializerProvider provider) throws IOException,
                JsonProcessingException {
    
            String jsonValue = "null";
            try
            {
                if(value != null) {             
                    double lat = value.getY();
                    double lon = value.getX();
                    jsonValue = String.format("POINT (%s %s)", lat, lon);
                }
            }
            catch(Exception e) {}
    
            jgen.writeString(jsonValue);
        }
    
    }
    
  • 创建这样的反序列化程序:

    import java.io.IOException;
    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.DeserializationContext;
    import com.fasterxml.jackson.databind.JsonDeserializer;
    import com.vividsolutions.jts.geom.Coordinate;
    import com.vividsolutions.jts.geom.GeometryFactory;
    import com.vividsolutions.jts.geom.Point;
    import com.vividsolutions.jts.geom.PrecisionModel;
    
    public class JsonToPointDeserializer extends JsonDeserializer<Point> {
    
        private final static GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 26910); 
    
        @Override
        public Point deserialize(JsonParser jp, DeserializationContext ctxt)
                throws IOException, JsonProcessingException {
    
            try {
                String text = jp.getText();
                if(text == null || text.length() <= 0)
                    return null;
    
                String[] coordinates = text.replaceFirst("POINT ?\\(", "").replaceFirst("\\)", "").split(" ");
                double lat = Double.parseDouble(coordinates[0]);
                double lon = Double.parseDouble(coordinates[1]);
    
                Point point = geometryFactory.createPoint(new Coordinate(lat, lon));
                return point;
            }
            catch(Exception e){
                return null;
            }
        }
    
    }
    

也许你也可以用this serializerthis deserializer,也可以用here.

Postgresql相关问答推荐

如何在Common Lisp中使用Postmodern在表更改时获得通知?

在SqlalChemy中转义动态CamelCase场

我需要一个变量来引用上周的星期五

多克.波斯格雷斯.PgAdmin4

我应该如何更新热门表?

JOOQ:数据库版本早于 COCKROACHDB 支持的方言:13.0.0

需要用 jack/pgx 更新 golang 中复合类型的 PSQL 行

如何计算每月的出版物数量?

查找最小值和最大值

Django + Postgres + 大时间序列

推送到 Heroku 时出现带有 Postgres 的 Rails 迁移错误

升级到 OSX Mavericks 后修复 postgresql

SQLAlchemy 中使用什么类型存储字节字符串?

如何从 WSL 连接到 windows postgres 数据库

将 PostgreSQL 配置为仅适用于 LOCALHOST 或指定的 ip + 端口

查询仅属于特定部门的用户

无法使用 sequelize 从本地 node 应用程序连接到 heroku postgresql 数据库

理解 Postgres 行大小

Hibernate 启动很慢

Array Push的 Postgres 数组追加和数组长度