moxy json介绍_MOXy的对象图和动态JAXB
moxy json介紹
JAXB(JSR-222)使您可以輕松地將域類的實(shí)例轉(zhuǎn)換為XML。 EclipseLink MOXy實(shí)現(xiàn)提供了一個(gè)稱為Dynamic JAXB的擴(kuò)展,在其中,您可以使用諸如DynamicEntity之類的映射實(shí)例代替真實(shí)的類??。 您可以使用采用屬性名稱的get和set方法(即customer.get(“ address”)和customer.set('name“,” Jane Doe“)訪問(wèn)DynamicEntity上的數(shù)據(jù)。在本文中,我們首先將基于外部映射文件引導(dǎo)動(dòng)態(tài)JAXBContext ,然后將XML文檔解組到動(dòng)態(tài)實(shí)體,最后將應(yīng)用對(duì)象圖來(lái)確定結(jié)果JSON輸出的范圍。
您可以從2013年3月24日開(kāi)始從每晚下載EclipseLink 2.5.0每晚下載,以嘗試使用此功能:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
動(dòng)態(tài)Java模型
對(duì)于靜態(tài)模型,元數(shù)據(jù)是從Java類派生的,并通過(guò)提供的任何元數(shù)據(jù)進(jìn)行擴(kuò)充(請(qǐng)參閱: JAXB –不需要注釋 )。 由于在MOXy的動(dòng)態(tài)JAXB中沒(méi)有域類,因此類型必須完全由元數(shù)據(jù)定義。 可以從XML模式完成此操作,也可以在本示例中使用MOXy的外部映射文檔完成此操作。
oxm.xml
由于沒(méi)有真正的Java類,因此在外部映射文檔中,我們需要指定每個(gè)映射,并為每個(gè)映射指定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提供程序。 對(duì)于動(dòng)態(tài)JAXB,此文件的內(nèi)容與使用MOXy時(shí)的通常內(nèi)容略有不同(與將EclipseLink MOXy指定為JAXB Provider相比 )。 該文件與域模型放在同一包中,因?yàn)檫@里有一個(gè)虛擬域模型, jaxb.properties文件將是blog.objectgraphs.dynamic包中唯一的真實(shí)項(xiàng)目。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory示范代碼
下面我們將探討使用對(duì)象圖的兩種不同方法。
演示–通過(guò)元數(shù)據(jù)指定的對(duì)象圖
在下面的演示代碼中,我們將利用外部映射文檔中定義的對(duì)象圖。 對(duì)象圖是為動(dòng)態(tài)模型定義的,與為相應(yīng)的靜態(tài)模型完全一樣(請(qǐng)參見(jiàn): MOXy的對(duì)象圖– XML和JSON的輸入/輸出局部模型 )。 唯一不同的是,我們從解組調(diào)用中獲得的對(duì)象是DynamicEntity的實(shí)例,而不是Customer的實(shí)例。
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);}}演示–以編程方式創(chuàng)建的對(duì)象圖
在下面的演示代碼中,我們將以編程方式創(chuàng)建對(duì)象圖。 對(duì)象圖是為動(dòng)態(tài)模型創(chuàng)建的,與為相應(yīng)的靜態(tài)模型創(chuàng)建的完全一樣(請(qǐng)參閱: MOXy的對(duì)象圖譜-從XML和JSON快速往返于部分模型 )。 不同之處在于,我們使用動(dòng)態(tài)實(shí)體的名稱而非類來(lái)創(chuàng)建對(duì)象圖,并且從解組調(diào)用中獲取了DynamicEntity的實(shí)例而不是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);}}輸入輸出
對(duì)于元數(shù)據(jù)驅(qū)動(dòng)的演示和程序化演示,以下輸入和輸出相同。
input.xml /輸出
我們將使用以下文檔來(lái)填充我們的域模型。 我們還將撤回封送它,以證明所有內(nèi)容均已實(shí)際映射。
<?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>基于對(duì)象圖的XML輸出
下面的XML由與上一個(gè)XML文檔完全相同的模型生成。 區(qū)別在于我們利用對(duì)象圖來(lái)選擇映射內(nèi)容的子集。
<?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>基于對(duì)象圖的JSON輸出
以下是與以JSON表示的先前XML文檔相同的子集。 我們使用了新的JSON_WRAPPER_AS_ARRAY_NAME屬性(請(qǐng)參閱綁定到JSON和XML –處理集合 )來(lái)改善集合值的表示形式。
{"name" : "Jane Doe","billingAddress" : {"city" : "Any Town","province" : "Ontario"},"phoneNumbers" : [ "555-1111", "555-2222" ] } 參考: Java XML和JSON綁定博客中的JCG合作伙伴 Blaise Doughan的MOXy的對(duì)象圖和動(dòng)態(tài)JAXB 。翻譯自: https://www.javacodegeeks.com/2013/04/moxys-object-graphs-dynamic-jaxb.html
moxy json介紹
總結(jié)
以上是生活随笔為你收集整理的moxy json介绍_MOXy的对象图和动态JAXB的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在线ddos测压平台注册流程(在线ddo
- 下一篇: jdk8运行jdk7的代码_即使在jdk