gpt4 book ai didi

java - 使用 Jackson 解析 geojson

转载 作者:行者123 更新时间:2023-11-30 04:35:26 27 4
gpt4 key购买 nike

我有一些与 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

最佳答案

出现此问题的原因是,它将变量的定义和变量的 setter 方法都视为 double x;setX(double x) 为它被定义了两次。您需要通过添加 @XmlAccessorType 来告诉它选择一个或另一个。在类(class)的顶部并将其设置为 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)

关于java - 使用 Jackson 解析 geojson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13633547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com