javascript
moxy json介绍_MOXy的@XmlVariableNode – JSON模式示例
moxy json介紹
我們正在向EclipseLink MOXy添加從域模型生成JSON模式的 功能 。 為此,我們創建了一個新的變量節點映射。 在本文中,我將通過將Java模型映射到JSON模式來演示新的映射。
您可以使用每晚構建的EclipseLink 2.6.0進行嘗試:
- http://www.eclipse.org/eclipselink/downloads/nightly.php
JSON模式(input.json / Output)
以下是摘自http://json-schema.org/examples.html的“基本示例”。 請注意,該類型具有許多屬性,但它們不會顯示為JSON數組。 相反,它們顯示為鍵入在屬性名稱上的單獨的JSON對象。
{"title": "Example Schema","type": "object","properties": {"firstName": {"type": "string"},"lastName": {"type": "string"},"age": {"description": "Age in years","type": "integer","minimum": 0}},"required": ["firstName", "lastName"] }Java模型
以下是我們用于此示例的Java模型。
JsonSchema(存儲在列表中的屬性)
在JSON模式的Java表示中,我們有一個類,它具有Property對象的集合。 而不是集合的默認表示形式(請參閱: 綁定到JSON&XML –處理集合 ),我們希望每個Property以其名稱作為鍵。 我們可以使用@XmlVariableNode批注進行此操作。 通過它,我們可以指定目標對象的字段/屬性,該字段/屬性應用作鍵。
package blog.variablenode.jsonschema;import java.util.*; import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlVariableNode;@XmlAccessorType(XmlAccessType.FIELD) public class JsonSchema {private String title;private String type;@XmlElementWrapper@XmlVariableNode("name")public List<Property> properties;private List<String> required;}JsonSchema(存儲在地圖中的屬性)
在此版本的JsonSchema類中,我們將屬性的類型從List <Property>屬性更改為Map <String,Property> 。 注釋保持不變,所不同的是,當@XmlVariableNode是在地圖中使用的變量節點名稱作為地圖的關鍵。
package blog.variablenode.jsonschema;import java.util.*; import javax.xml.bind.annotation.*; import org.eclipse.persistence.oxm.annotations.XmlVariableNode;@XmlAccessorType(XmlAccessType.FIELD) public class JsonSchema {private String title;private String type;@XmlElementWrapper@XmlVariableNode("name")public Map<String, Property> properties;private List<String> required;}屬性
為了防止將名稱字段編組,我們需要使用@XmlTransient對其進行注釋(請參見JAXB和Unmapped屬性 )。
package blog.variablenode.jsonschema;import javax.xml.bind.annotation.*;@XmlAccessorType(XmlAccessType.FIELD) public class Property {@XmlTransientprivate String name;private String description;private String type;private Integer minimum;}示范代碼
下面是一些示例代碼,您可以用來證明一切正常。
package blog.variablenode.jsonschema;import java.util.*; import javax.xml.bind.*; import javax.xml.transform.stream.StreamSource; import org.eclipse.persistence.jaxb.JAXBContextProperties;public class Demo {public static void main(String[] args) throws Exception {Map<String, Object> properties = new HashMap<String, Object>();properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);JAXBContext jc = JAXBContext.newInstance(new Class[] {JsonSchema.class}, properties);Unmarshaller unmarshaller = jc.createUnmarshaller();StreamSource json = new StreamSource("src/blog/variablenode/jsonschema/input.json");JsonSchema jsonSchema = unmarshaller.unmarshal(json, JsonSchema.class).getValue();Marshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(jsonSchema, System.out);}}外部元數據
MOXy還提供了一個外部映射文檔,使您可以為第三方對象提供元數據或為模型應用替代映射(請參閱:將對象映射到多個XML模式–天氣示例 )。 以下是此示例的映射文檔。
<?xml version="1.0"?> <xml-bindingsxmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"package-name="blog.variablenode.jsonschema"xml-accessor-type="FIELD"><java-types><java-type name="JsonSchema"><java-attributes><xml-variable-node java-attribute="properties" java-variable-attribute="name"><xml-element-wrapper/></xml-variable-node></java-attributes></java-type><java-type name="Property"><java-attributes><xml-transient java-attribute="name"/></java-attributes></java-type></java-types> </xml-bindings> 參考: 來自MOXy的@XmlVariableNode –來自我們的JCG合作伙伴 Blaise Doughan的JSON模式示例 ,位于Java XML&JSON Binding博客上。翻譯自: https://www.javacodegeeks.com/2013/06/moxys-xmlvariablenode-json-schema-example.html
moxy json介紹
總結
以上是生活随笔為你收集整理的moxy json介绍_MOXy的@XmlVariableNode – JSON模式示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 步步高电脑系统如何(步步高的系统)
- 下一篇: sudo su su_Spring Se