Java 如何正确使用 Jackson 的 JaxbAnnotationIntrospector?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22433679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Hymanson's JaxbAnnotationIntrospector correctly?
提问by jbuhacoff
I'm able to generate the XML I want using @HymansonXmlProperty annotations with the default mapper configuration. But my classes are generated by maven-jaxb2-plugin and already have the @XmlAttribute annotations. When I try using the JaxbAnnotationIntrospector it serializes the attributes as child elements. What am I doing wrong?
我能够使用带有默认映射器配置的 @HymansonXmlProperty 注释生成我想要的 XML。但是我的类是由 maven-jaxb2-plugin 生成的,并且已经有了 @XmlAttribute 注释。当我尝试使用 JaxbAnnotationIntrospector 时,它将属性序列化为子元素。我究竟做错了什么?
Expected output: <problem xmlns="" id="aaa"><description>test</description></problem>
(repeatable with testGenerateXmlCorrect)
预期输出:(<problem xmlns="" id="aaa"><description>test</description></problem>
可与 testGenerateXmlCorrect 重复)
Actual output: <problem xmlns=""><id>aaa</id><description>test</description></problem>
(repeatable with testGenerateXmlWrong)
实际输出:(<problem xmlns=""><id>aaa</id><description>test</description></problem>
可与 testGenerateXmlWrong 重复)
I can also generate the expected XML using JAXB but this question is how to do it with Hymanson using the JaxbAnnotationIntrospector.
我也可以使用 JAXB 生成预期的 XML,但这个问题是如何使用 JaxbAnnotationIntrospector 与 Hymanson 一起做。
Junit test:
Junit测试:
import com.fasterxml.Hymanson.core.JsonProcessingException;
import com.fasterxml.Hymanson.dataformat.xml.XmlMapper;
import com.fasterxml.Hymanson.dataformat.xml.annotation.HymansonXmlProperty;
import com.fasterxml.Hymanson.dataformat.xml.annotation.HymansonXmlRootElement;
import com.fasterxml.Hymanson.module.jaxb.JaxbAnnotationIntrospector;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import org.junit.Test;
public class JaxbAttributeTest {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JaxbAttributeTest.class);
@XmlRootElement(name="problem")
public static class ProblemJaxb {
@XmlAttribute(name="id")
public String id;
public String description;
}
@Test
public void testGenerateXmlWrong() throws JsonProcessingException {
ProblemJaxb problem = new ProblemJaxb();
problem.id = "aaa";
problem.description = "test";
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(xmlMapper.getTypeFactory()));
log.debug("ProblemJaxb: {}", xmlMapper.writeValueAsString(problem));
}
@HymansonXmlRootElement(localName="problem")
public static class ProblemHymanson {
@HymansonXmlProperty(isAttribute=true)
public String id;
public String description;
}
@Test
public void testGenerateXmlCorrect() throws JsonProcessingException {
ProblemHymanson problem = new ProblemHymanson();
problem.id = "aaa";
problem.description = "test";
XmlMapper xmlMapper = new XmlMapper();
log.debug("ProblemHymanson: {}", xmlMapper.writeValueAsString(problem));
}
}
Classpath includes:
类路径包括:
- com.fasterxml.Hymanson.core:Hymanson-core:jar:2.3.2
- com.fasterxml.Hymanson.core:Hymanson-annotations:jar:2.3.2
- com.fasterxml.Hymanson.core:Hymanson-databind:jar:2.3.2
- com.fasterxml.Hymanson.dataformat:Hymanson-dataformat-xml:jar:2.3.2
- com.fasterxml.Hymanson.module:Hymanson-module-jaxb-annotations:jar:2.3.2
- org.codehaus.woodstox:stax2-api:jar:3.1.1
- javax.xml.stream:stax-api:jar:1.0-2
- com.sun.xml.bind:jaxb-impl:jar:2.2.7
- com.sun.xml.bind:jaxb-core:jar:2.2.7
- javax.xml.bind:jaxb-api:jar:2.2.7
- com.fasterxml.Hymanson.core:Hymanson-core:jar:2.3.2
- com.fasterxml.Hymanson.core:Hymanson-annotations:jar:2.3.2
- com.fasterxml.Hymanson.core:Hymanson-databind:jar:2.3.2
- com.fasterxml.Hymanson.dataformat:Hymanson-dataformat-xml:jar:2.3.2
- com.fasterxml.Hymanson.module:Hymanson-module-jaxb-annotations:jar:2.3.2
- org.codehaus.woodstox:stax2-api:jar:3.1.1
- javax.xml.stream:stax-api:jar:1.0-2
- com.sun.xml.bind:jaxb-impl:jar:2.2.7
- com.sun.xml.bind:jaxb-core:jar:2.2.7
- javax.xml.bind:jaxb-api:jar:2.2.7
By the way I also tried configuring the XmlMapper with this:
顺便说一下,我还尝试使用以下配置 XmlMapper:
xmlMapper.getSerializationConfig().with(new JaxbAnnotationIntrospector(xmlMapper.getTypeFactory()));
but that generated even worse output because the root element name was incorrect: <ProblemJaxb xmlns=""><id>aaa</id><description>test</description></ProblemJaxb>
但这产生了更糟糕的输出,因为根元素名称不正确: <ProblemJaxb xmlns=""><id>aaa</id><description>test</description></ProblemJaxb>
采纳答案by Sotirios Delimanolis
It looks like this problem existed before, but was not reproducible by the writers of Hymanson. It doesn't seem like the bug report went very far.
看起来这个问题以前存在过,但Hyman逊的作家无法重现。错误报告似乎并没有走得太远。
I was able to solve the problem by using XmlJaxbAnnotationIntrospector
instead of JaxbAnnotationIntrospector
.
我能够通过使用XmlJaxbAnnotationIntrospector
代替JaxbAnnotationIntrospector
.