我有一个类,其中包含来自vividsolutions的Geometry对象。此类没有默认的0参数构造函数,因为它只能由工厂实例化。
所以我用@XmlJavaTypeAdapter(GeometryXmlAdapter.class)注释了类中的几何对象,告诉它我想自己处理编组和解组。
@XmlRootElement
@Entity
@Table(name = "fm_point")
public class GeoCodePoint {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
/**
* The point encoded as ST_POINT in PostGIS / H2 GeoDB
*/
@XmlJavaTypeAdapter(GeometryXmlAdapter.class)
@Column(columnDefinition = "geometry(Point,4326)", nullable = true)
@Type(type = "org.hibernate.spatial.GeometryType")
private Geometry point;
这是XmlAdapter:
public class GeometryXmlAdapter extends XmlAdapter<String, Geometry> {
@Override
public Geometry unmarshal(String simpleFeatureString) throws Exception {
Geometry geometry = null;
try {
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
WKTReader wktReader = new WKTReader(geometryFactory);
geometry = wktReader.read(simpleFeatureString);
} catch (Exception e) {
e.printStackTrace();
}
return geometry;
}
@Override
public String marshal(Geometry geometry) throws Exception {
return geometry.toText();
}
}
对于两个进程,编组和解组,XmlAdapter都可以工作。但是在工厂帮助下从String成功生成几何对象后解组的情况下我得到了这个错误:
java.lang.NoSuchMethodException: com.vividsolutions.jts.geom.Geometry.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at com.sun.xml.internal.bind.v2.ClassFactory.create0(Unknown Source)
就像代码调用了我的XmlAdapter(debuging证明它进入了方法)之后,它忽略了我的几何对象,并试图将它自己解组。
我在这里想念一下吗?