我理解所有關於如何使用XMLAdapters到convert unmappable types,或只是改變某些對象如何序列化/反序列化爲XML。如果我使用註釋(包級別或其他),一切都很好。問題是我正在嘗試更改第三方對象的表示,我無法更改源代碼(即爲了注入註釋)。JAXB XML適配器通過註釋工作,但不通過setAdapter
考慮到Marshaller對象有一個manually adding adapters的方法,這應該不成問題。不幸的是,無論我做什麼,我都無法以這種方式設置適配器來「踢入」。例如,我有一個類表示XYZ空間中的點(地心座標)。在我生成的XML中,我希望將其轉換爲lat/long/altitude(大地座標)。這裏是我的課:
地心
package testJaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class GeocentricCoordinate {
// Units are in meters; see http://en.wikipedia.org/wiki/Geocentric_coordinates
private double x;
private double y;
private double z;
@XmlAttribute
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
@XmlAttribute
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@XmlAttribute
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
}
大地
package testJaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @see http://en.wikipedia.org/wiki/Geodetic_system
*/
@XmlRootElement
public class GeodeticCoordinate {
private double latitude;
private double longitude;
// Meters
private double altitude;
public GeodeticCoordinate() {
this(0,0,0);
}
public GeodeticCoordinate(double latitude, double longitude, double altitude) {
super();
this.latitude = latitude;
this.longitude = longitude;
this.altitude = altitude;
}
@XmlAttribute
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
@XmlAttribute
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@XmlAttribute
public double getAltitude() {
return altitude;
}
public void setAltitude(double altitude) {
this.altitude = altitude;
}
}
GeocentricToGeodeticLocationAdapter
package testJaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
* One of our systems uses xyz coordinates to represent locations. Consumers of our XML would much
* prefer lat/lon/altitude. This handles converting between xyz and lat lon alt.
*
* @author ndunn
*
*/
public class GeocentricToGeodeticLocationAdapter extends XmlAdapter<GeodeticCoordinate,GeocentricCoordinate> {
@Override
public GeodeticCoordinate marshal(GeocentricCoordinate arg0) throws Exception {
// TODO: do a real coordinate transformation
GeodeticCoordinate coordinate = new GeodeticCoordinate();
coordinate.setLatitude(45);
coordinate.setLongitude(45);
coordinate.setAltitude(1000);
return coordinate;
}
@Override
public GeocentricCoordinate unmarshal(GeodeticCoordinate arg0) throws Exception {
// TODO do a real coordinate transformation
GeocentricCoordinate gcc = new GeocentricCoordinate();
gcc.setX(100);
gcc.setY(200);
gcc.setZ(300);
return gcc;
}
}
ObjectWithLocation場
package testJaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class ObjectWithLocation {
private GeocentricCoordinate location = new GeocentricCoordinate();
public GeocentricCoordinate getLocation() {
return location;
}
public void setLocation(GeocentricCoordinate location) {
this.location = location;
}
public static void main(String[] args) {
ObjectWithLocation object = new ObjectWithLocation();
try {
JAXBContext context = JAXBContext.newInstance(ObjectWithLocation.class, GeodeticCoordinate.class, GeocentricCoordinate.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setAdapter(new GeocentricToGeodeticLocationAdapter());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, System.out);
}
catch (JAXBException jaxb) {
jaxb.printStackTrace();
}
}
}
輸出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectWithLocation>
<location z="0.0" y="0.0" x="0.0"/>
</objectWithLocation>
通過使用註釋(在我package-info.java
文件):
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters
({
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=GeocentricToGeodeticLocationAdapter.class,type=GeocentricCoordinate.class),
})
package package testJaxb;
我碰到下面的(所需)XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectWithLocation>
<location longitude="45.0" latitude="45.0" altitude="1000.0"/>
</objectWithLocation>
所以我的問題是雙重的。
- 爲什麼適配器在註釋時工作,但在通過
setAdapter
方法顯式設置時不工作? - 當我有無法註解的類和無法修改其package-info.java以添加註釋的問題時,我該如何解決此問題?
好啊。這很煩人。我現在看到,我沒有足夠好的閱讀addAdapter的合同。是否有一個原因不暴露'void addAdapter(類 classToAdapt,XMLAdapter )'方法? –
I82Much
2011-05-24 14:49:24
@ I82Much - 可以增強JAXB以提供該行爲。目前它不支持這個的原因主要是由於性能。 JAXBContext可以在創建時初始化其元數據,併爲每個元帥/解組操作使用相同的元數據。如果允許在Marshaller/Unmarshaller級別引入適配器,則JAXB impl需要考慮這種元數據更改的可能性。 – 2011-05-24 15:35:31
確定非常合理的迴應。我只是很煩,因爲在XStream中進行這種定製非常容易,而且我發現有更多的障礙需要通過JAXB來完成。我會檢查MOXY。謝謝您的幫助。 – I82Much 2011-05-24 16:49:53