gpt4 book ai didi

java - 在相同情况下 @XmlJavaTypeAdapter 被忽略

转载 作者:行者123 更新时间:2023-11-30 04:11:20 28 4
gpt4 key购买 nike

当调用 JAXB.marshal 时,我使用 AdapterDataObj 类将 DataObj 类转换为 AdaptedDataObj。


package test;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(AdapterDataObj.class)
public class DataObj {
public String bla = "I'm DataObj";
public String name;
public DataObj(String name) {
this.name = name;
}

}

package test;
public class AdaptedDataObj {
public String bla="I'm AdaptedDataObj";
public String name;

public AdaptedDataObj(String name) {
this.name = name;
}
}

package test;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class AdapterDataObj extends XmlAdapter {
@Override
public DataObj unmarshal(AdaptedDataObj v) throws Exception {
return null; // not needed in this case
}

@Override
public AdaptedDataObj marshal(DataObj v) throws Exception {
System.out.println("Marschal for " + v.name + " called!");
return new AdaptedDataObj(v.name);
}
}

现在我将 DataObj 放入一个新类中,并为该类调用 JAXB.marshal。输出看起来不错。我还收到调用 AdaptedDataObj 的消息。


package test;
import javax.xml.bind.JAXB;
public class Testobj {
public DataObj x;
public Testobj() {
x = new DataObj("Hallo World");
}

public static void main(String[] args) throws Exception {
System.out.println("Testobj");
System.out.println("=================================");
Testobj to = new Testobj();
JAXB.marshal(to, System.out);
}
}

现在我将“public DataObj x;”更改为“public Object x;”,因为我不仅想在该类中存储 DataObj。

但在这种情况下,不再调用 AdapterDataObj。 DataObj 而不是 AdapterDataObj 导出为 xml。

知道问题出在哪里吗?

我也尝试将 @XmlJavaTypeAdapter 添加到 package-info.java 中,但没有成功。 AdapterDataObj 仍未被调用。我已经使用 Java 1.7.0_45 和 1.8.0-ea 进行了测试。


package test;
import javax.xml.bind.JAXB;
import javax.xml.bind.annotation.XmlSeeAlso;
@XmlSeeAlso({DataObj.class})
public class Testobj3 {
public Object x;
public Testobj3() {
x = new DataObj("Hallo World");
}

public static void main(String[] args) throws Exception {
System.out.println("Testobj3");
System.out.println("=================================");
Testobj3 to3 = new Testobj3();
JAXB.marshal(to3, System.out);
}
}

最佳答案

根据JAXB 2.2 specification :

A @XmlJavaTypeAdapter that extends XmlAdapter and is specified on the class, interface or Enum type (i.e. on a program element that matches meta annotation @Target={type}) must adapt boundType at the point of reference as follows:

  1. a property/field whose reference type is boundType
  2. a property/field where boundType is used as a parametric type

因此字段/属性必须是给定类型,以便 JAXB 识别需要使用适配器。

出于同样的原因,您不能在根元素上使用适配器。解释可以在已关闭的 JAXB 问题 JAXB-117 中找到。 :

Adapters are defined against types, not instances. In particular, the adapter is designed in such a way that it's not always possible to infer the right adapter just from an instance.

编辑在 JAXB 类中使用 Object 并不常见。也许您要存储的对象有一些共同点,以便您可以提取可以使用 @XmlJavaTypeAdapter 进行注释的接口(interface)/基类。如果它们没有任何共同点,也许最好将它们存储为单独的属性。

关于java - 在相同情况下 @XmlJavaTypeAdapter 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19552057/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com