moxy json介绍_MOXy的对象图和动态JAXB
moxy json介紹
JAXB(JSR-222)使您可以輕松地將域類的實例轉換為XML。 EclipseLink MOXy實現提供了一個稱為Dynamic JAXB的擴展,在其中,您可以使用諸如DynamicEntity之類的映射實例代替真實的類??。 您可以使用采用屬性名稱的get和set方法(即customer.get(“ address”)和customer.set('name“,” Jane Doe“)訪問DynamicEntity上的數據。在本文中,我們首先將基于外部映射文件引導動態JAXBContext ,然后將XML文檔解組到動態實體,最后將應用對象圖來確定結果JSON輸出的范圍。
您可以從2013年3月24日開始從每晚下載EclipseLink 2.5.0每晚下載,以嘗試使用此功能:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
動態Java模型
對于靜態模型,元數據是從Java類派生的,并通過提供的任何元數據進行擴充(請參閱: JAXB –不需要注釋 )。 由于在MOXy的動態JAXB中沒有域類,因此類型必須完全由元數據定義。 可以從XML模式完成此操作,也可以在本示例中使用MOXy的外部映射文檔完成此操作。
oxm.xml
由于沒有真正的Java類,因此在外部映射文檔中,我們需要指定每個映射,并為每個映射指定Java屬性的類型。
<?xml version="1.0"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"package-name="blog.objectgraphs.dynamic"><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" type="java.lang.Integer"/><xml-element java-attribute="name" type="java.lang.String"/><xml-element java-attribute="billingAddress" type="blog.objectgraphs.dynamic.Address"/><xml-element java-attribute="shippingAddress" type="blog.objectgraphs.dynamic.Address"/><xml-element java-attribute="phoneNumbers" name="phoneNumber" type="blog.objectgraphs.dynamic.PhoneNumber" container-type="java.util.List"><xml-element-wrapper/></xml-element></java-attributes></java-type><java-type name="Address"><java-attributes><xml-element java-attribute="street" type="java.lang.String"/><xml-element java-attribute="city" type="java.lang.String"/><xml-element java-attribute="province" type="java.lang.String"/><xml-element java-attribute="postalCode" type="java.lang.String"/></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" type="java.lang.String"/><xml-value java-attribute="value" type="java.lang.String"/></java-attributes></java-type></java-types> </xml-bindings>jaxb.properties
jaxb.properties文件用于指定JAXB提供程序。 對于動態JAXB,此文件的內容與使用MOXy時的通常內容略有不同(與將EclipseLink MOXy指定為JAXB Provider相比 )。 該文件與域模型放在同一包中,因為這里有一個虛擬域模型, jaxb.properties文件將是blog.objectgraphs.dynamic包中唯一的真實項目。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory示范代碼
下面我們將探討使用對象圖的兩種不同方法。
演示–通過元數據指定的對象圖
在下面的演示代碼中,我們將利用外部映射文檔中定義的對象圖。 對象圖是為動態模型定義的,與為相應的靜態模型完全一樣(請參見: MOXy的對象圖– XML和JSON的輸入/輸出局部模型 )。 唯一不同的是,我們從解組調用中獲得的對象是DynamicEntity的實例,而不是Customer的實例。
package blog.objectgraphs.dynamic;import java.io.File; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.dynamic.DynamicEntity; import org.eclipse.persistence.jaxb.JAXBContextProperties; import org.eclipse.persistence.jaxb.MarshallerProperties;public class DemoMetadata {public static void main(String[] args) throws Exception {Map<String, Object> properties = new HashMap<String, Object>(1);properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "blog/objectgraphs/dynamic/oxm.xml");JAXBContext jc = JAXBContext.newInstance("blog.objectgraphs.dynamic", DemoMetadata.class.getClassLoader(), properties);Unmarshaller unmarshaller = jc.createUnmarshaller();File xml = new File("src/blog/objectgraphs/dynamic/input.xml");DynamicEntity customer = (DynamicEntity) 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);}}演示–以編程方式創建的對象圖
在下面的演示代碼中,我們將以編程方式創建對象圖。 對象圖是為動態模型創建的,與為相應的靜態模型創建的完全一樣(請參閱: MOXy的對象圖譜-從XML和JSON快速往返于部分模型 )。 不同之處在于,我們使用動態實體的名稱而非類來創建對象圖,并且從解組調用中獲取了DynamicEntity的實例而不是Customer 。
package blog.objectgraphs.dynamic;import java.io.File; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.dynamic.DynamicEntity; import org.eclipse.persistence.jaxb.JAXBContextProperties; import org.eclipse.persistence.jaxb.JAXBHelper; import org.eclipse.persistence.jaxb.MarshallerProperties; import org.eclipse.persistence.jaxb.ObjectGraph; import org.eclipse.persistence.jaxb.Subgraph;public class DemoRuntime {public static void main(String[] args) throws Exception {Map<String, Object> properties = new HashMap<String, Object>(1);properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "blog/objectgraphs/dynamic/oxm.xml");JAXBContext jc = JAXBContext.newInstance("blog.objectgraphs.dynamic", DemoMetadata.class.getClassLoader(), properties);Unmarshaller unmarshaller = jc.createUnmarshaller();File xml = new File("src/blog/objectgraphs/dynamic/input.xml");DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xml);// Output XMLMarshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(customer, System.out);// Create the Object GraphObjectGraph contactInfo = JAXBHelper.getJAXBContext(jc).createObjectGraph("blog.objectgraphs.dynamic.Customer");contactInfo.addAttributeNodes("name");Subgraph location = contactInfo.addSubgraph("billingAddress");location.addAttributeNodes("city", "province");Subgraph simple = contactInfo.addSubgraph("phoneNumbers");simple.addAttributeNodes("value");// Output XML - Based on Object Graphmarshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, contactInfo);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 –處理集合 )來改善集合值的表示形式。
{"name" : "Jane Doe","billingAddress" : {"city" : "Any Town","province" : "Ontario"},"phoneNumbers" : [ "555-1111", "555-2222" ] } 參考: Java XML和JSON綁定博客中的JCG合作伙伴 Blaise Doughan的MOXy的對象圖和動態JAXB 。翻譯自: https://www.javacodegeeks.com/2013/04/moxys-object-graphs-dynamic-jaxb.html
moxy json介紹
總結
以上是生活随笔為你收集整理的moxy json介绍_MOXy的对象图和动态JAXB的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线ddos测压平台注册流程(在线ddo
- 下一篇: 环评如何网上备案(环评网上备案怎么办理)