【问题标题】:Google Endpoints transformer for org.geolatte.geom.Pointorg.geolatte.geom.Point 的 Google Endpoints 转换器
【发布时间】:2019-03-19 17:24:20
【问题描述】:

我有一个实体,其中一个属性是位置org.geolatte.geom.Point<G2D>。我为其创建了 Google Endpoints Transformer<Point<G2D>, String>,但收到以下错误:

com.fasterxml.jackson.databind.JsonMappingException: 直接自引用导致循环(通过引用链:com.example.package.MyEntity["location"]->org.geolatte.geom.Point["envelope" ]->org.geolatte.geom.Envelope["coordinateReferenceSystem"]->org.geolatte.geom.crs.Geographic2DCoordinateReferenceSystem["coordinateSystem"]->org.geolatte.geom.crs.EllipsoidalCoordinateSystem2D["axes"]->org .geolatte.geom.crs.GeodeticLongitudeCSAxis["unit"]->org.geolatte.geom.crs.AngularUnit["fundamentalUnit"]->org.geolatte.geom.crs.AngularUnit["fundamentalUnit"])

Jackson 为何无法转换资产,应该如何转换?

【问题讨论】:

    标签: java jackson google-cloud-endpoints


    【解决方案1】:

    org.geolatte.geom.Point 类扩展了org.geolatte.geom.Geometry,它具有Envelope<P> getEnvelope() 方法。 Jackson 默认序列化所有 POJO getters: get*is* 方法。您需要使用 JsonIgnore 注释忽略这些方法。示例MixIn 接口可能如下所示:

    interface GeometryMixIn {
    
        @JsonIgnore
        Envelope getEnvelope();
    
        @JsonIgnore
        PositionSequence getPositions();
    }
    

    现在我们需要如下注册:

    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixIn(Geometry.class, GeometryMixIn.class);
    

    现在您可以使用此映射器对Point 进行序列化。如果其他getters 有问题,请以同样的方式忽略它们。但最好的OOP 方法是创建自定义POJO,我们将根据Point 创建它,我们可以完全控制3-rd party libraries 可见的内容。

    【讨论】:

      【解决方案2】:

      我通过将org.geolatte.geom.Point<G2D> 替换为org.locationtech.jts.geom.Point 解决了我的问题,但我不知道为什么Point 不能正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-18
        • 2020-01-02
        • 2013-05-23
        • 1970-01-01
        • 2013-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多