简体   繁体   English

Apache CXF JAXB Marshaller没有正确编组@XmlAttribute

[英]Apache CXF JAXB Marshaller not marshalling @XmlAttribute correctly

I am trying to get an XML response from my CXF version 3.1 based REST service in java in the following format. 我试图从以下格式的基于我的基于CXF 3.1版的REST服务获取XML响应。

<root>
 <element a="X" b="1.2" c="3.2"/>
 <element a="Y" b="5.5" c="1.4"/>
 <element a="Z" b="54.2" c="55.4"/>
</root>

I have defined my DTO as below : 我已将DTO定义如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="root")
public class Root{

    private List<Element> element;

    ---getters setters and no arg constructor--
}
@XmlType(propOrder = {
    "a","b","c"})
@XmlRootElement(name="element")
public class Element{

    @XmlAttribute
    private String a;

    @XmlAttribute
    private Double b;

    @XmlAttribute
    private Double c;

    ---Getter Setter---

}

I have not overriden any mapper, so default CXF JAXB xml mapper is in action. 我没有覆盖任何映射器,因此默认的CXF JAXB xml映射器正在运行。

But my xml response is coming as :- 但我的xml响应如下: -

<root>
   <element>
      <element>
          <a>X</a>
          <b>1.2</b>
          <c>3.2</c>
       </element>
       <element>
          <a>Y</a>
          <b>5.5</b>
          <c>1.4</c>
       </element>
       <element>
          <a>Z</a>
          <b>54.2</b>
          <c>55.4</c>
       </element>
    </element>
</root>

I have checked the Moxy API option as well, but that i cant use. 我也检查了Moxy API选项,但我无法使用。 Can someone please help that what is wrong with my code or what is missing ? 有人可以请帮助我的代码有什么问题或缺少什么?

Thanks in advance 提前致谢

After spending quite some time, i found the problem and then figurred out the solution as well. 花了很长时间后,我发现了问题,然后找出了解决方案。

Problem 问题

The problem was that apache cxf by default marshaller ie JAXB was not honouring the attribute annotations and hence was represnting the fields as elements instead of attributes. 问题是apache cxf默认为marshaller,即JAXB没有遵守属性注释,因此将字段重新表示为元素而不是属性。

Solution

Since by default serialization and de-serialization mechanism ie JAXB was not working hence we need to override this. 由于默认的序列化和反序列化机制,即JAXB不起作用,因此我们需要覆盖它。 And here my problem got solved by using Jackson as the serialization and de-serialization api. 在这里,我的问题通过使用Jackson作为序列化和反序列化api得到了解决。

Jackson starting with 2.0 verison provide both JSON as well as XML formats, and have exposed xml specific annotations for the same. Jackson以2.0版本开头提供JSON和XML格式,并为此提供了特定的xml注释。

Use @JacksonXmlProperty with attribute set as true and then overriding the ObjectMapper bean as below solved the problem. 使用@JacksonXmlProperty,将属性设置为true,然后重写ObjectMapper bean,如下所示解决了问题。

@Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper= new ObjectMapper();
        //overriding apache cxf JAXB mapper to use jackson mapper.
        AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
        objectMapper.setAnnotationIntrospector(introspector);
        return objectMapper;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM