JAXB XML Adapters通过注释工作,但不通过setAdapter工作 [英] JAXB XML Adapters work via annotations but not via setAdapter

查看:154
本文介绍了JAXB XML Adapters通过注释工作,但不通过setAdapter工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全了解如何使用 XMLAdapters 转换不可映射的类型,或者只是改变某些对象被序列化/反序列化为XML的方式。如果我使用注释(包级别或其他),一切都很好。问题是我试图改变第三方对象的表示,我无法将源代码更改为(即为了注入注释)。

I understand all about how to use XMLAdapters to convert unmappable types, or just to change how certain objects are serialized/deserialized to XML. Everything works great if I use annotations (either package level or otherwise). The problem is that I am attempting to change the representations of third party objects which I cannot change the source code to (i.e. in order to inject annotations).

那应该是'这是一个问题,考虑到Marshaller对象有一个方法手动添加适配器。不幸的是,无论我做什么,我都无法通过这种方式设置适配器来启动。例如,我有一个表示XYZ空间中的点的类(地心坐标)。在我生成的XML中,我希望将其转换为lat / long / altitude(大地坐标)。以下是我的课程:

That shouldn't be a problem, considering that the Marshaller object has a method for manually adding adapters. Unfortunately, no matter what I do, I can't get the adapters set in this way to "kick in". For instance, I have a class representing a point in XYZ space (geocentric coordinates). In the XML I produce, I want this to be converted into lat/long/altitude (geodetic coordinates). Here are my classes:

Geocentric

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 file):

By using an annotation (in my package-info.java file):

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters
({
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=GeocentricToGeodeticLocationAdapter.class,type=GeocentricCoordinate.class),
})

package package testJaxb;

我得到以下(所需)xml

I get the following (desired) xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectWithLocation>
    <location longitude="45.0" latitude="45.0" altitude="1000.0"/>
</objectWithLocation>

所以我的问题是双重的。

So my question is twofold.


  1. 为什么适配器在注释时有效,但是在通过 setAdapter 方法显式设置时却没有?

  2. 如何当我有一些我无法注释的类和我的package-info.java我无法修改以添加注释时,我能解决这个问题吗?

  1. Why does the adapter work when annotated, but not when explicitly set via the setAdapter method?
  2. How do I work around this problem when I have classes that I cannot annotate and whose package-info.java I cannot modify in order to add the annotations?


推荐答案

Marshaller 上的 setAdapter(XmlAdapter)用于传入已初始化的XmlAdapter,以获取已使用 @XmlJavaTypeAdapter 注释的属性。以下链接是我利用此行为的答案:

The setAdapter(XmlAdapter) on Marshaller is used to pass in an initialized XmlAdapter for a property that is already annotated with @XmlJavaTypeAdapter. The link below is to an answer where I leverage this behaviour:

  • Using JAXB to cross reference XmlIDs from two XML files

如果你想映射第三方类,可以使用 EclipseLink JAXB(MOXy)的XML映射文件(我是MOXy主管):

If you want to map third party classes you could use EclipseLink JAXB (MOXy)'s XML mapping file (I'm the MOXy lead):

  • http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html

这篇关于JAXB XML Adapters通过注释工作,但不通过setAdapter工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆