简体   繁体   English

如何在 Spring Boot 中反序列化/序列化类型 Geometry?

[英]How to deserialize / serialize type Geometry in spring boot?

I have an entity with attributes of type MultiPolygon and Point;我有一个具有 MultiPolygon 和 Point 类型属性的实体; so I'm making a get request but this is returning a SerializationException.所以我发出了一个获取请求,但这返回了一个 SerializationException。

I researched it and saw that I have to put some notes, create a configuration class and put the corresponding dependency in pom.xml.研究了一下,看到要放一些笔记,创建一个配置类,把对应的依赖放到pom.xml中。 Follow as I did below:按照我在下面做的:

Entity:实体:

package com.zxventures.model;

@Entity
@Table(name = "pdv")
public class PDV implements Serializable {

private static final long serialVersionUID = 1L;

 @Column(name="coverage_area")
 @JsonSerialize(using = GeometrySerializer.class)
 @JsonDeserialize(contentUsing = GeometryDeserializer.class)
 private MultiPolygon coverageArea;

 @Column(name="address")
 @JsonSerialize(using = GeometrySerializer.class)
 @JsonDeserialize(contentUsing = GeometryDeserializer.class)
 private Point address;
}

Config class:配置类:

package com.zxventures.config;

@Configuration
public class JacksonConfig {

 @Bean
 public JtsModule jtsModule() {
  return new JtsModule();
 }
}

pom.xml: pom.xml:

<dependency>
<groupId>com.bedatadriven</groupId>
<artifactId>jackson-datatype-jts</artifactId>
<version>2.4</version>
</dependency>

The exception occurs:出现异常:

could not deserialize; nested exception is 
org.hibernate.type.SerializationException: could not deserialize

I think I'm missing some code but I can not detect it;我想我错过了一些代码,但我无法检测到它; I think I put all the code I saw in similar questions.我想我把我看到的所有代码都放在了类似的问题中。

我遇到了同样的问题,并将下面的行添加到 application.properties 中,然后它就可以工作了。

spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect

You're using Spatial data types, so need to include below dependency to work您正在使用空间数据类型,因此需要包含以下依赖项才能工作

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-spatial</artifactId>
</dependency>

And change dialect accordingly eg org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect并相应地更改方言,例如org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect

See Spatial data types请参阅空间数据类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM