未检测到XmlJavaTypeAdapter

未检测到XmlJavaTypeAdapter,java,jaxb,Java,Jaxb,对于JAXB专家来说,希望这是一个简单的例子: 我试图封送一个不可修改的类,它没有定义默认的无参数构造函数。我已经定义了一个XmlAdapter实现,但它似乎没有被接受。我提出了一个简单的自包含示例,但仍然无法奏效。谁能告诉我哪里做错了 不可变类 @XmlJavaTypeAdapter(FooAdapter.class) @XmlRootElement public class Foo { private final String name; private final int age;

对于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<AdaptedFoo, Foo> {
  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; }
}
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.<init>(JAXBContextImpl.java:283)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(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)
堆栈跟踪

public class FooAdapter extends XmlAdapter<AdaptedFoo, Foo> {
  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; }
}
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.<init>(JAXBContextImpl.java:283)
        at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(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)
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1次illegalannotationException计数
Foo没有无参数默认构造函数。
此问题与以下位置有关:
在福
位于com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(IllegalAnnotationsException.java:91)
位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:451)
位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:283)
位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:126)
位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1142)
位于com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:130)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:601)
位于javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
位于javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
位于javax.xml.bind.ContextFinder.find(ContextFinder.java:445)
位于javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
位于javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
位于Marshal2.main(Marshal2.java:11)

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

以下内容应该会有所帮助:

FOO作为根对象

在类型级别指定
@XmlJavaTypeAdapter
时,它仅适用于引用该类的字段/属性,而不适用于该类的实例是XML树中的根对象时。这意味着您必须自己将
Foo
转换为
AdaptedFoo
,并在
AdaptedFoo
上创建
JAXBContext
,而不是
Foo

封送

package forum11966714;

import javax.xml.bind.*;

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

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

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

        jaxbMarshaller.marshal(new AdaptedFoo(foo), System.out);              
      } catch (JAXBException e) {
        e.printStackTrace();
      }   
    }
  }
适配食物

您需要将
@XmlRootElement
注释添加到
AdaptedFoo
类中。您可以从
Foo
类中删除相同的注释

package forum11966714;

import javax.xml.bind.annotation.*;

@XmlRootElement
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;
    }
}

FOO作为嵌套对象

Foo
不是根对象时,一切都按照映射它的方式工作。我已经扩展了您的模型来演示这是如何工作的

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();
    }   
  }
}
package forum11966714;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Bar {

    private Foo foo;

    public Foo getFoo() {
        return foo;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

}
演示

请注意,在引导
JAXBContext
时,JAXB参考实现不允许您指定
Foo

package forum11966714;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {
    public static void main(String[] args) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Bar.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            File xml = new File("src/forum11966714/input.xml");
            Bar bar = (Bar) jaxbUnmarshaller.unmarshal(xml);

            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(bar, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}
input.xml/Output

<?xml version="1.0" encoding="UTF-8"?>
<bar>
    <foo name="Jane Doe" age="35"/>
</bar>

我知道情况并非如此,但如果在字段中放置@XmlJavaTypeAdapter时出现此类错误,请检查是否指定了名称空间。你可能需要它

在我的情况下,这不起作用:

@XmlElement(name = "Expiration")
@XmlJavaTypeAdapter(DateAdapter.class)
private Date expiration;
在指定命名空间之前:

@XmlElement(name = "Expiration", namespace="http://site/your.namespace")
@XmlJavaTypeAdapter(DateAdapter.class)
private Date expiration;

谢谢布莱斯!(我想我最初看的是你的博客)。看起来问题是我在引导过程中将Foo.class指定给JAXBContext。遗憾的是,JAXB在这一点上无法工作,因为我已经为Foo指定了一个适配器。。。使引导有点复杂。@b尽管你似乎是这个关于编组/解编的难题的专家)也许你可以在我的问题上给我一些指导。我有一个XML,它的根元素是
,里面有很多单类型元素,例如