MOXy的对象图– XML和JSON的输入/输出局部模型
假設您有一個要公開為RESTful服務的域模型。 問題是您只想輸入/輸出部分數據。 以前,您將創建一個代表子集的單獨模型,然后使用代碼在模型之間移動數據。 在EclipseLink 2.5.0中,我們有一個稱為“對象圖”的新功能,使您能夠輕松地在模型上定義局部視圖。
您可以從2013年3月24日開始從每晚下載EclipseLink 2.5.0每晚下載,以嘗試這一點:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
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 {@XmlAttributeprivate 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 {@XmlAttributeprivate String type;@XmlValueprivate 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 XMLMarshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(customer, System.out);// Output XML - Based on Object Graphmarshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, 'contact info');marshaller.marshal(customer, System.out);// Output JSON - Based on Object Graphmarshaller.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 –處理集合 ),以改善集合值的表示形式。
外部元數據
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綁定博客中。
翻譯自: https://www.javacodegeeks.com/2013/03/moxys-object-graphs-inputoutput-partial-models-to-xml-json.html
總結
以上是生活随笔為你收集整理的MOXy的对象图– XML和JSON的输入/输出局部模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos接口api(对接ddos的api
- 下一篇: 编写linux程序(linux 下编程)