EclipseLink MOXy不编组用@XMLAttribute注释的子图字段

时间:2014-11-04 18:31:50

标签: jaxb eclipselink moxy

问题标题是自我解释的。我可以在这里找到的示例中复制此问题: http://blog.bdoughan.com/2013/03/moxys-object-graphs-inputoutput-partial.html

如果你只是改变

@XmlNamedObjectGraph(
    name="simple",
    attributeNodes={
        @XmlNamedAttributeNode("value"),
    }
)

@XmlNamedObjectGraph(
    name="simple",
    attributeNodes={
        @XmlNamedAttributeNode("type"),
    }
)

然后在marsahalled输出中,<phoneNumber>元素为空且没有属性。

我希望将type视为<phoneNumber>元素的属性。

我使用的是EclipseLink MOXy 2.5.0版本

输入:

<?xml version="1.0" encoding="UTF-8"?>
<customer id="123">
   <name>Jane Doe</name>
   <billingAddress>
      <street>1 A Street</street>
      <city>Any Town</city>
      <province>Ontario</province>
      <postalCode>A1B 2C3</postalCode>
   </billingAddress>
   <shippingAddress>
      <street>2 B Road</street>
      <city>Another Place</city>
      <province>Quebec</province>
      <postalCode>X7Y 8Z9</postalCode>
   </shippingAddress>
   <phoneNumbers>
      <phoneNumber type="work">555-1111</phoneNumber>
      <phoneNumber type="home">555-2222</phoneNumber>
   </phoneNumbers>
</customer>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<customer id="123">
   <name>Jane Doe</name>
   <billingAddress>
      <city>Any Town</city>
      <province>Ontario</province>
   </billingAddress>
   <phoneNumbers>
      <phoneNumber/>
      <phoneNumber/>
   </phoneNumbers>
</customer>

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<customer id="123">
   <name>Jane Doe</name>
   <billingAddress>
      <city>Any Town</city>
      <province>Ontario</province>
   </billingAddress>
   <phoneNumbers>
      <phoneNumber type="work" />
      <phoneNumber type="home" />
   </phoneNumbers>
</customer>

课程代码(从上述链接粘贴的复制品):

客户:

package blog.objectgraphs.metadata;

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlNamedObjectGraph(
    name="contact info",
    attributeNodes={
        @XmlNamedAttributeNode("name"),
        @XmlNamedAttributeNode(value="billingAddress", subgraph="location"),
        @XmlNamedAttributeNode(value="phoneNumbers", subgraph="simple")
    },
    subgraphs={
        @XmlNamedSubgraph(
            name="location",
            attributeNodes = { 
                @XmlNamedAttributeNode("city"),
                @XmlNamedAttributeNode("province")
            }
        )
    }
)
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlAttribute
    private int id;

    private String name;

    private Address billingAddress;

    private Address shippingAddress;

    @XmlElementWrapper
    @XmlElement(name="phoneNumber")
    private List<PhoneNumber> phoneNumbers;

}

地址:

package blog.objectgraphs.metadata;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Address {

    private String street;

    private String city;

    private String province;

    private String postalCode;

}

电话号码:

package blog.objectgraphs.metadata;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlNamedObjectGraph(
    name="simple",
    attributeNodes={
        @XmlNamedAttributeNode("value"),
    }
)
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {

    @XmlAttribute
    private String type;

    @XmlValue
    private String value;

}

演示:

package blog.objectgraphs.metadata;

import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/blog/objectgraphs/metadata/input.xml");
        Customer customer = (Customer) unmarshaller.unmarshal(xml);

        // Output XML
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(customer, System.out);

        // Output XML - Based on Object Graph
        marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "contact info");
        marshaller.marshal(customer, System.out);

        // Output JSON - Based on Object Graph
        marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
        marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
        marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
        marshaller.marshal(customer, System.out);
    }

}

1 个答案:

答案 0 :(得分:2)

您看到的问题是由于EclipseLink 2.5.0版本中的以下错误:

此问题已在EclipseLink 2.5.1版本中修复,可从以下链接获取:

使用EclipseLink 2.5.1,您将获得以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <name>Jane Doe</name>
   <billingAddress>
      <city>Any Town</city>
      <province>Ontario</province>
   </billingAddress>
   <phoneNumbers>
      <phoneNumber type="work"/>
      <phoneNumber type="home"/>
   </phoneNumbers>
</customer>

更新

  只有1个区别,早些时候它曾用于编组phoneNumbers列表   JSON为“phoneNumbers”:[“555-1111”,“555-2222”]但是现在呢   marshalls为“phoneNumbers”:[{“value”:“555-1111”},{“value”:   “555-2222”}]。新的逻辑上更加一致   表示对象图的真实本质,但我可以想到   旧格式可能更有用的情况。例如,如果你   想要发送ID列表(XMLIDRefs),然后将它们作为JSON发送   整数数组看起来比发送JSON对象数组更好   而每个

又包含一个整数

这是我们做出的有目的的改变。大多数用户更喜欢具有一致的行为。现在的方式是,无论是否应用过滤器,您都可以使用相同的机制来获取值。

如果POJO中唯一的映射属性为@XmlValue,则不使用value密钥。