我们正在向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>
翻译自: https://www.javacodegeeks.com/2013/06/moxys-xmlvariablenode-json-schema-example.html