java – JAXB XML适配器通过注释工作,但不通过setAdapter

前端之家收集整理的这篇文章主要介绍了java – JAXB XML适配器通过注释工作,但不通过setAdapter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我了解所有关于如何使用 XMLAdaptersconvert unmappable types,或者只是改变某些对象如何序列化/反序列化为XML.如果我使用注释(包级别或其他方式),一切都会很好.问题是我正在尝试更改第三方对象的表示,我不能将源代码更改为(即为了注入注释).

这不应该是一个问题,考虑到Marshaller对象有一个方法manually adding adapters.不幸的是,无论我做什么,我不能得到这样的方式设置为“踢”.例如,我有一个类代表XYZ空间中的一个点(地心坐标).在我生产的XML中,我希望将其转换为lat / long / altitude(大地坐标).这是我的课程:

地心

  1. package testJaxb;
  2.  
  3. import javax.xml.bind.annotation.XmlAttribute;
  4. import javax.xml.bind.annotation.XmlRootElement;
  5.  
  6. @XmlRootElement
  7. public class GeocentricCoordinate {
  8. // Units are in meters; see http://en.wikipedia.org/wiki/Geocentric_coordinates
  9. private double x;
  10. private double y;
  11. private double z;
  12.  
  13. @XmlAttribute
  14. public double getX() {
  15. return x;
  16. }
  17. public void setX(double x) {
  18. this.x = x;
  19. }
  20. @XmlAttribute
  21. public double getY() {
  22. return y;
  23. }
  24. public void setY(double y) {
  25. this.y = y;
  26. }
  27. @XmlAttribute
  28. public double getZ() {
  29. return z;
  30. }
  31. public void setZ(double z) {
  32. this.z = z;
  33. }
  34. }

大地

  1. package testJaxb;
  2. import javax.xml.bind.annotation.XmlAttribute;
  3. import javax.xml.bind.annotation.XmlRootElement;
  4. /**
  5. * @see http://en.wikipedia.org/wiki/Geodetic_system
  6. */
  7. @XmlRootElement
  8. public class GeodeticCoordinate {
  9.  
  10. private double latitude;
  11. private double longitude;
  12. // Meters
  13. private double altitude;
  14.  
  15. public GeodeticCoordinate() {
  16. this(0,0);
  17. }
  18.  
  19. public GeodeticCoordinate(double latitude,double longitude,double altitude) {
  20. super();
  21. this.latitude = latitude;
  22. this.longitude = longitude;
  23. this.altitude = altitude;
  24. }
  25.  
  26. @XmlAttribute
  27. public double getLatitude() {
  28. return latitude;
  29. }
  30. public void setLatitude(double latitude) {
  31. this.latitude = latitude;
  32. }
  33.  
  34. @XmlAttribute
  35. public double getLongitude() {
  36. return longitude;
  37. }
  38.  
  39. public void setLongitude(double longitude) {
  40. this.longitude = longitude;
  41. }
  42.  
  43. @XmlAttribute
  44. public double getAltitude() {
  45. return altitude;
  46. }
  47. public void setAltitude(double altitude) {
  48. this.altitude = altitude;
  49. }
  50.  
  51.  
  52.  
  53. }

GeocentricToGeodeticLocationAdapter

  1. package testJaxb;
  2. import javax.xml.bind.JAXBContext;
  3. import javax.xml.bind.JAXBException;
  4. import javax.xml.bind.Marshaller;
  5. import javax.xml.bind.annotation.adapters.XmlAdapter;
  6.  
  7.  
  8. /**
  9. * One of our systems uses xyz coordinates to represent locations. Consumers of our XML would much
  10. * prefer lat/lon/altitude. This handles converting between xyz and lat lon alt.
  11. *
  12. * @author ndunn
  13. *
  14. */
  15. public class GeocentricToGeodeticLocationAdapter extends XmlAdapter<GeodeticCoordinate,GeocentricCoordinate> {
  16.  
  17. @Override
  18. public GeodeticCoordinate marshal(GeocentricCoordinate arg0) throws Exception {
  19. // TODO: do a real coordinate transformation
  20. GeodeticCoordinate coordinate = new GeodeticCoordinate();
  21. coordinate.setLatitude(45);
  22. coordinate.setLongitude(45);
  23. coordinate.setAltitude(1000);
  24. return coordinate;
  25. }
  26.  
  27. @Override
  28. public GeocentricCoordinate unmarshal(GeodeticCoordinate arg0) throws Exception {
  29. // TODO do a real coordinate transformation
  30. GeocentricCoordinate gcc = new GeocentricCoordinate();
  31. gcc.setX(100);
  32. gcc.setY(200);
  33. gcc.setZ(300);
  34. return gcc;
  35. }
  36. }

ObjectWithLocation字段

  1. package testJaxb;
  2. import javax.xml.bind.JAXBContext;
  3. import javax.xml.bind.JAXBException;
  4. import javax.xml.bind.Marshaller;
  5. import javax.xml.bind.annotation.XmlRootElement;
  6.  
  7. @XmlRootElement
  8. public class ObjectWithLocation {
  9.  
  10. private GeocentricCoordinate location = new GeocentricCoordinate();
  11.  
  12. public GeocentricCoordinate getLocation() {
  13. return location;
  14. }
  15.  
  16. public void setLocation(GeocentricCoordinate location) {
  17. this.location = location;
  18. }
  19.  
  20.  
  21. public static void main(String[] args) {
  22.  
  23. ObjectWithLocation object = new ObjectWithLocation();
  24.  
  25. try {
  26. JAXBContext context = JAXBContext.newInstance(ObjectWithLocation.class,GeodeticCoordinate.class,GeocentricCoordinate.class);
  27. Marshaller marshaller = context.createMarshaller();
  28.  
  29. marshaller.setAdapter(new GeocentricToGeodeticLocationAdapter());
  30. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
  31.  
  32. marshaller.marshal(object,System.out);
  33.  
  34. }
  35. catch (JAXBException jaxb) {
  36. jaxb.printStackTrace();
  37. }
  38. }
  39. }

输出

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <objectWithLocation>
  3. <location z="0.0" y="0.0" x="0.0"/>
  4. </objectWithLocation>

通过使用注释(在我的package-info.java文件中):

  1. @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters
  2. ({
  3. @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=GeocentricToGeodeticLocationAdapter.class,type=GeocentricCoordinate.class),})
  4.  
  5. package package testJaxb;

我得到以下(所需)xml

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

所以我的问题是双重的.

>为什么适配器在注释时工作,但是通过setAdapter方法显式设置时不适用?
>如果我有类不能注释的类,并且我的package-info.java我不能修改添加注释,我该如何解决这个问题?

解决方法

Marshaller上的setAdapter(XmlAdapter)用于传递已经使用@XmlJavaTypeAdapter注释的属性的初始化XmlAdapter.下面的链接是一个我利用这个行为的答案:

> Using JAXB to cross reference XmlIDs from two XML files

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

> @L_301_5@

猜你在找的Java相关文章