1

我有一些对应于 PostGIS Point 对象的 GeoJSON。

这是一个例子:

{"type":"Point","coordinates":[-397408.355686851020437,7575590.819041009992361]}

我尝试通过执行以下操作将其解析为 Java 中的 postgis 点对象:

                ObjectMapper mapper = new ObjectMapper();
                Point coors = null;
                 try {
                     coors = mapper.readValue(str, Point.class);
                } catch (JsonParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JsonMappingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

我得到以下 JsonMappingException:

org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:346)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:321)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:167)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:188)
    at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2820)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2719)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
    at dao.impl.PostGisDaoImpl.getOsmPoiNodes(PostGisDaoImpl.java:53)
    at service.OsmDBService.main(OsmDBService.java:24)
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
    at org.codehaus.jackson.map.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:194)
    at org.codehaus.jackson.map.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:1065)
    at org.codehaus.jackson.map.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:654)
    at org.codehaus.jackson.map.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:583)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:432)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:341)
    ... 8 more

有谁知道这里有什么问题?谢谢

编辑:

新例外:

org.codehaus.jackson.map.JsonMappingException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:346)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:321)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:167)
    at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:596)
    at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:438)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:383)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:321)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:167)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:188)
    at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2820)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2719)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1854)
    at dao.impl.PostGisDaoImpl.getOsmPoiNodes(PostGisDaoImpl.java:53)
    at service.OsmDBService.main(OsmDBService.java:24)
Caused by: java.lang.IllegalArgumentException: Conflicting setter definitions for property "x": org.postgis.Point#setX(1 params) vs org.postgis.Point#setX(1 params)
    at org.codehaus.jackson.map.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:194)
    at org.codehaus.jackson.map.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:1065)
    at org.codehaus.jackson.map.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:654)
    at org.codehaus.jackson.map.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:583)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:432)
    at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:341)
    ... 14 more
4

2 回答 2

1

问题的原因是它既看到了变量的定义,也看到了变量的 setter 方法,double x;并且setX(double x)因为它被定义了两次。您需要告诉它选择一个或另一个,方法是@XmlAccessorType在类的顶部添加 并将其设置为XmlAccessType.FIELD或您喜欢使用的任何一个。例如:

@XmlAccessorType(XmlAccessType.FIELD)
public class Point {
  ...
}


如果您Point从这里使用它与我的预期不同。这是因为它实际上有两个 set 方法:void setX(double x)void setX(int x)

因此,在这种情况下,您需要字段访问。仍在寻找如何以编程方式而不是注释来设置它。

更新:我认为在这种情况下,您最好的选择是为Point该类创建一个包装器,然后您可以将其拉回。所以像:

public class MyPoint {

    private String type;

    @JsonIgnore
    public Point point;

    public MyPoint(){
        this.point = new Point();
    }

    public double[] getCoordinates(){
        return new double[] {this.point.getX(), this.point.getY()};
    }

    public void setCoordinates(double[] x){
        if (x.length == 2){
            this.point.setX(x[0]);
            this.point.setY(x[1]);
        }
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

然后将您的代码更改为:

ObjectMapper mapper = new ObjectMapper();
Point coors = null;
 try {
     MyPoint mine = mapper.readValue(str, MyPoint.class);
     coors = mine.point;
} catch (JsonParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JsonMappingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

也许有更好的方法,但这应该有效。

编辑:

这是我运行的测试,看看这是否有效:

public static void main(String[] args) {
    String str = "{\"type\":\"Point\",\"coordinates\":[-397408.355686851020437,7575590.819041009992361]}";

    ObjectMapper mapper = new ObjectMapper();
    Point coors = null;
     try {
         MyPoint mine = mapper.readValue(str, MyPoint.class);
         coors = mine.point;
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     System.out.println(coors);
}

我得到的输出:

POINT(-397408.355686851 7575590.81904101)
于 2012-11-29T20:04:04.790 回答
1

异常指出了问题,您可以从链接到的 Javadoc 中看到。有两个备用二传手:

void setX(double x)
void setX(int x) 

杰克逊不想猜测它应该尝试使用哪一个。拥有一个字段和设置器是没有问题的;方法(setter)优先于字段。

由于您无法修改Point,您可能需要使用混合注释。或者如果您愿意,可以自定义解串器。

于 2012-11-30T04:15:48.757 回答