姬長信(Redy)

未检测到XmlJavaTypeAdapter


希望对JAXB专家来说很简单:

我试图编组一个不定义默认无参数构造函数的不可变类.我已经定义了一个XmlAdapter实现,但它似乎没有被选中.我已经整理了一个简单的自包含示例,但仍然无法正常工作.谁能告诉我我做错了什么?

不变的阶级

@XmlJavaTypeAdapter(FooAdapter.class)
@XmlRootElement
public class Foo {
  private final String name;
  private final int age;

  public Foo(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() { return name; }
  public int getAge() { return age; }
}

适配器和值类型

public class FooAdapter extends XmlAdapter {
  public Foo unmarshal(AdaptedFoo af) throws Exception {
    return new Foo(af.getName(), af.getAge());
  }

  public AdaptedFoo marshal(Foo foo) throws Exception {
    return new AdaptedFoo(foo);
  }
}

class AdaptedFoo {
  private String name;
  private int age;

  public AdaptedFoo() {}

  public AdaptedFoo(Foo foo) {
    this.name = foo.getName();
    this.age = foo.getAge();
  }

  @XmlAttribute
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  @XmlAttribute
  public int getAge() { return age; }
  public void setAge(int age) { this.age = age; }
}

编组

public class Marshal {
  public static void main(String[] args) {
    Foo foo = new Foo("Adam", 34);

    try {
      JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

      // output pretty printed
      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

      jaxbMarshaller.marshal(foo, System.out);              
    } catch (JAXBException e) {
      e.printStackTrace();
    }   
  }
}

堆栈跟踪

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Foo does not have a no-arg default constructor.
        this problem is related to the following location:
                at Foo

        at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:283)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:126)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142)
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
        at Marshal2.main(Marshal2.java:11)

请注意,我使用的是JDK 1.7.0_05.