Reputation: 2579
I have some GeoJSON which corresponds to a PostGIS Point object.
Here is an example:
{"type":"Point","coordinates":[-397408.355686851020437,7575590.819041009992361]}
I tried to parse this into a postgis point object in Java by doing:
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();
}
I get the following 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
Does anyone know what is wrong here? Thanks
EDIT:
New Exception:
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
Upvotes: 1
Views: 5601
Reputation: 14192
The reason for the issue is that it sees both the definition of the variable an the setter method for the variable so double x;
and setX(double x)
as it being defined twice. You need to tell it to pick one or another by adding the @XmlAccessorType
at the top of your class and setting it to XmlAccessType.FIELD
or whichever you prefer it to use. For example:
@XmlAccessorType(XmlAccessType.FIELD)
public class Point {
...
}
Point
from here it is different than what I expected. It is because it actually has two set methods: void setX(double x)
and void setX(int x)
So in this case you want field access. Still looking to see how to set it programmatically instead of annotation.
UPDATE: I think in this case your best bet is to create a wrapper for the Point
class that you can then pull the point back out of. So something like:
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;
}
}
And then change your code to:
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();
}
Maybe there is a better way, but this should work.
EDIT:
This is the test I ran to see if this worked:
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);
}
And the output that I get:
POINT(-397408.355686851 7575590.81904101)
Upvotes: 1
Reputation: 116472
Exception points out the problem, which you can see from the Javadoc you linked to. There are two alternate setters:
void setX(double x)
void setX(int x)
and Jackson doesn't want to guess which one it should try to use. Having a field and setter is NOT problematic; methods (setter) have precedence over fields.
Since you can't modify Point
, you will probably want to use mix-in annotations.
Or custom deerializer if you prefer.
Upvotes: 1