moxy json介绍
假设您有一个要公开为RESTful服务的域模型。 问题是您只想输入/输出部分数据。 以前,您将创建一个代表子集的单独模型,然后使用代码在模型之间移动数据。 在EclipseLink 2.5.0中,我们有一个称为“对象图”的新功能,使您可以轻松地在模型上定义局部视图。
您可以从2013年3月24日开始从每晚下载EclipseLink 2.5.0每晚下载,以尝试使用此功能:
Java模型
以下是我们将用于此示例的Java模型。 该模型表示客户数据。 我们将使用对象图输出仅足够的信息,以便有人可以通过电话与客户联系。
顾客
@XmlNamedObjectGraph扩展用于指定我们希望编组/解组的模型的子集。 通过指定一个或多个@XmlNamedAttributeNode批注来完成。 如果要将对象图应用于属性,则可以为其指定子图。 可以将子图定义为目标类上的@XmlNamedSubgraph或@XmlNamedObjectGraph 。
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;
}
地址
因为我们将Address类的对象图定义为Customer类的子图,所以在这里我们不需要做任何事情。
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;
}
电话号码
对于Customer类上的phoneNumbers属性,我们指定应使用一个名为simple的对象图来限定数据范围。 我们将在PhoneNumber类上定义此对象图。 这种方法的优点是它使对象图更易于重用。
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;
}
示范代码
演示版
在下面的演示代码中,我们将阅读一个XML文档以完全填充我们的Java模型。 在将其编组起来以证明所有内容都已完全映射之后,我们将在封送拆收器上指定一个对象图(第22行),并将子集输出到XML和JSON。
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);
}
}
input.xml /输出
我们将使用以下文档来填充我们的域模型。 我们还将撤回封送它,以证明所有内容均已实际映射。
<?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输出
下面的XML由与上一个XML文档完全相同的模型生成。 不同之处在于,我们利用命名对象图来选择映射内容的子集。
<?xml version='1.0' encoding='UTF-8'?>
<customer>
<name>Jane Doe</name>
<billingAddress>
<city>Any Town</city>
<province>Ontario</province>
</billingAddress>
<phoneNumbers>
<phoneNumber>555-1111</phoneNumber>
<phoneNumber>555-2222</phoneNumber>
</phoneNumbers>
</customer>
基于对象图的JSON输出
以下是与以JSON表示的先前XML文档相同的子集。 我们已经使用了新的
JSON_WRAPPER_AS_ARRAY_NAME属性(请参阅绑定到JSON&XML –处理集合 )以改善集合值的表示形式。
{
'name' : 'Jane Doe',
'billingAddress' : {
'city' : 'Any Town',
'province' : 'Ontario'
},
'phoneNumbers' : [ '555-1111', '555-2222' ]
}
外部元数据
MOXy还提供了一个外部绑定文档,使您可以为第三方对象提供元数据或为模型应用备用映射(请参阅:将对象映射到多个XML模式–天气示例 )。 以下是此示例的映射文档。
<?xml version='1.0'?>
<xml-bindings xmlns='http://www.eclipse.org/eclipselink/xsds/persistence/oxm'
package-name='blog.objectgraphs.metadata'
xml-accessor-type='FIELD'>
<java-types>
<java-type name='Customer'>
<xml-named-object-graphs>
<xml-named-object-graph name='contact info'>
<xml-named-attribute-node name='name'/>
<xml-named-attribute-node name='billingAddress' subgraph='location'/>
<xml-named-attribute-node name='phoneNumbers' subgraph='simple'/>
<xml-named-subgraph name='location'>
<xml-named-attribute-node name='city'/>
<xml-named-attribute-node name='province'/>
</xml-named-subgraph>
</xml-named-object-graph>
</xml-named-object-graphs>
<xml-root-element/>
<java-attributes>
<xml-attribute java-attribute='id'/>
<xml-element java-attribute='phoneNumbers' name='phoneNumber'>
<xml-element-wrapper/>
</xml-element>
</java-attributes>
</java-type>
<java-type name='PhoneNumber'>
<xml-named-object-graphs>
<xml-named-object-graph name='simple'>
<xml-named-attribute-node name='value'/>
</xml-named-object-graph>
</xml-named-object-graphs>
<java-attributes>
<xml-attribute java-attribute='type'/>
<xml-value java-attribute='value'/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
参考:来自Java的 JCG合作伙伴 Blaise Doughan的MOXy的对象图– XML和JSON的输入/输出局部模型,位于Java XML&JSON绑定博客中。
moxy json介绍