JAVA适配器简单代码注释,java – JAXB XML适配器通过注释工作,但不通过setAdapter

我了解所有关于如何使用

XMLAdapters到

convert unmappable types,或者只是改变某些对象如何序列化/反序列化为XML.如果我使用注释(包级别或其他方式),一切都会很好.问题是我正在尝试更改第三方对象的表示,我不能将源代码更改为(即为了注入注释).

这不应该是一个问题,考虑到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);

}

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 {

@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();

}

}

}

通过使用注释(在我的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

所以我的问题是双重的.

>为什么适配器在注释时工作,但是通过setAdapter方法显式设置时不适用?

>如果我有类不能注释的类,并且我的package-info.java我不能修改以添加注释,我该如何解决这个问题?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值