未检测到 XmlJavaTypeAdapter [英] XmlJavaTypeAdapter not being detected
问题描述
希望对 JAXB 专家来说是一个简单的方法:
Hopefully an easy one for JAXB experts:
我正在尝试编组一个不不定义默认无参数构造函数的不可变类.我已经定义了一个 XmlAdapter
实现,但它似乎没有被选中.我整理了一个简单的独立示例,但仍然无法正常工作.谁能告诉我我做错了什么?
I am trying to marshal an immutable class that does not define a default no-arg constructor. I have defined an XmlAdapter
implementation but it doesn't seem to be picked up. I have put together a simple self-contained example, which is still failing to work. Can anyone advise what I'm doing wrong?
不可变类
@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; }
}
Marshaller
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.<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)
请注意,我使用的是 JDK 1.7.0_05.
Note that I am using JDK 1.7.0_05.
推荐答案
以下内容应该会有所帮助:
The following should help:
FOO 作为根对象
当在类型级别指定 @XmlJavaTypeAdapter
时,它仅适用于引用该类的字段/属性,而不适用于该类的实例是 XML 树中的根对象时.这意味着您必须自己将 Foo
转换为 AdaptedFoo
,并在 AdaptedFoo
上创建 JAXBContext
而不是 <代码>Foo.
When @XmlJavaTypeAdapter
is specified at the type level it only applies to fields/properties referencing that class, and not when an instance of that class is a root object in your XML tree. This means that you will have to convert Foo
to AdaptedFoo
yourself, and create the JAXBContext
on AdaptedFoo
and not 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();
}
}
}
AdaptedFoo
您需要向 AdaptedFoo
类添加一个 @XmlRootElement
注释.您可以从 Foo
类中删除相同的注释.
You will need to add an @XmlRootElement
annotation to the AdaptedFoo
class. You can remove the same annotation from the Foo
class.
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
不是根对象时,一切都按照您映射的方式工作.我已经扩展了您的模型来演示这是如何工作的.
When Foo
isn't the root object everything works the way you have it mapped. I have extended your model to demonstrate how this would work.
条形
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
类.
Note that the JAXB reference implementation will not let you specify the Foo
class when bootstrapping the JAXBContext
.
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/输出
<?xml version="1.0" encoding="UTF-8"?>
<bar>
<foo name="Jane Doe" age="35"/>
</bar>
这篇关于未检测到 XmlJavaTypeAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!