繁体   English   中英

如何通过 Spring 引导发送 PostGiS 几何和点数据类型 Rest API

[英]How to send PostGiS Geometry and Point data types through Spring Boot Rest API

我有一个查询,它返回我从本机查询获得的接口 OverlayDTO。 center 是 PostGIS pointgeometry ,您可能已经猜到它是geometry

@Query("SELECT " +
        "o.geometry as geometry, " +
        "o.center as center, " +
        "FROM Overlay o " +
        "WHERE  o.layer.id = :layerId")
List<OverlayDTO> getAllOverlays(long layerId);

界面:

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;

public interface OverlayDTO {
    Geometry getGeometry();
    Point getCenter();
}

我想通过 REST API 像这样(如果可能的话)发送它(它应该调用一个服务并做更多的逻辑,但这是它的基本要点)

    @GetMapping(value="{layerId}")
public ResponseEntity<List<OverlayDTO>> getCompanyLayers(@PathVariable Long layerId) {
    List<OverlayDTO> overlays = mapRepository.getAllOverlays(layerId);
        
    return ResponseEntity.ok(overlays);
}

但是,我收到一个错误,即 Jackson 由于递归而无法 map。

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]->org.locationtech.jts.geom.Polygon["envelope"]

我该如何解决这个问题? 我应该以不同的方式查询数据,还是应该构建自己的 DTO class 并发送 POJO? 还是我需要构建一些序列化器/反序列化器?

您需要注册一个 Jackson 模块,该模块为 JTS Geometry 对象提供适当的序列化器和反序列化器。 一个不错的候选者是jackson-datatype-jts

如果您想了解如何执行此操作的示例,可以查看此博客文章 (我使用替代几何库编写它,但该方法应该适用于 JTS 和 jackson-datatype-jts 模块)。

有一种通用的交换格式 GeoJSON。 您可能必须编写 JTS 代码才能执行此操作,或者 PostGIS 具有直接从您的 DB 对象生成 GeoJSON 的功能。

另一种更简单的方法是将您的对象转换为 WKT 并以字符串的形式返回它们。 PostGIS 和 JTS 都可以从 WKT 生成和转换。 这可能会或可能不会更容易被其他服务使用,具体取决于进行调用的系统。 WKT 唯一令人讨厌的事情是假设了 SRID。 我已经创建了我的服务,以便有一个参数来请求客户端想要的 SRID(这当然要求您可以转换为请求的 SRID),或者让您的服务始终以事实上的标准返回数据,例如 4326。

暂无
暂无

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

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