JAXB XML Adapters 通过注释工作,但不通过 setAdapter 工作
我完全了解如何使用 XMLAdapters 转换不可映射的类型,或者只是更改某些对象序列化/反序列化为 XML 的方式。如果我使用注释(包级别或其他),一切都很有效。问题是我试图更改第三方对象的表示形式,而我无法更改源代码(即为了注入注释)。
考虑到 Marshaller 对象具有手动添加适配器的方法,这应该不是问题。不幸的是,无论我做什么,我都无法以这种方式设置适配器以“启动”。例如,我有一个类表示XYZ空间中的一个点(地心坐标)。在我生成的 XML 中,我希望将其转换为纬度/经度/高度(大地测量坐标)。这是我的课程:
地心
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;
}
}
地心到几何位置适配器
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;
}
}
“对象与位置”字段
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
- 当我有无法批注的类及其包信息时,如何解决此问题.java我无法修改以添加批注?