使用Jackson进行反序列化几何时出​​错 [英] Error de-serializing Geometry with Jackson

查看:368
本文介绍了使用Jackson进行反序列化几何时出​​错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring-Boot 2.2.1中使用PostGIS数据库实现一个简单的几何(GIS)控制器.

I am trying to implement a simple geometry (GIS) controller with PostGIS database in Spring-Boot 2.2.1.

当我尝试反序列化包含Point Geometry的任务实体时,出现错误:

When I try to de-serialize my task entity which contains the Point Geometry I am getting error:

There was an unexpected error (type=Internal Server Error, status=500).
Could not write JSON: org.locationtech.jts.geom.Point cannot be cast to com.vividsolutions.jts.geom.Geometry; nested exception is com.fasterxml.jackson.databind.JsonMappingException: org.locationtech.jts.geom.Point cannot be cast to com.vividsolutions.jts.geom.Geometry `enter code here`(through reference chain: java.util.ArrayList[0]->com.example.depdev.entity.Task["location"])

我的任务实体是:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer;
import com.bedatadriven.jackson.datatype.jts.serialization.GeometrySerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.locationtech.jts.geom.Point;

@Entity
@Table(name = "task")
public class Task {

public Task () {
};

@Id
private Long id;
private String title;
@Column(columnDefinition = "geometry(Point,4326)")
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(using = GeometryDeserializer.class)
private Point location;

// trimmed

我尝试将位置定义为点和几何,但是错误是相同的.

I have tried to define location as both a point and a geometry but the error is the same.

我的控制器能够保留一个新任务,但是当我尝试对其进行反序列化时,我得到了先前发布的错误:

My controller is able to persist a new task, but I get the previously posted error when I try to de-serialize it:

@GetMapping("/alltasks")
@ResponseBody
public List allTasks() throws JsonProcessingException {

    GeometryFactory gf = new GeometryFactory();
    Double y = -36.829;
    Double x = 174.896;

    Task testTask = new Task();
    testTask.setId(new Long(01));
    testTask.setTitle("Test Task");
    Point p = gf.createPoint(new Coordinate(x, y));
    p.setSRID(4326);
    testTask.setLocation(p);
    taskRepository.save(testTask);

    List<Task> listTasks = new ArrayList<>();
    listTasks = taskService.findAll();
    return listTasks;

我确实在此处中发布了递归错误-但是添加JacksonConfig类可以解决此错误.

I did have the recursion error that is posted here - but adding the JacksonConfig class has fixed this error.

为了完整起见,这里是Jackson的配置类:

For the sake of completeness here is the Jackson config class:

import com.bedatadriven.jackson.datatype.jts.JtsModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

我在 build.gradle 中的依赖项是:

dependencies {
implementation 'org.hibernate:hibernate-spatial'
compile group: 'org.locationtech.jts', name: 'jts-core', version: '1.16.0'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.0'
compile group: 'com.bedatadriven', name: 'jackson-datatype-jts', version: '2.4'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-ldap'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation 'org.springframework.security:spring-security-test'

我需要怎么做才能将任务实体转换为JSON而不会出现此错误?

What do I need to do to get the task entity to JSON without this error?

更新

我的代码中没有生动的解决方案的参考:

There is no reference in my code to vividsolutions:

推荐答案

正如其他指出的那样,仍然存在使用旧版本的点几何(vividsolutions.geom而不是较新的locationtech)的依赖项.

As other pointed out there is a dependency still using the old version of point geometry (vividsolutions.geom rather than the newer locationtech).

幸运的是,有人分支了jackson-databind.该分支将与定位技术配合使用:

Fortunately someone has branched jackson-databind. This branch will work with location tech :

https://mvnrepository.com/artifact/com.graphhopper.external/jackson-datatype-jts/0.10-2.5-1

这篇关于使用Jackson进行反序列化几何时出​​错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆