国内外网站开发现状,长安高端装备网站设计公司,做哪些网站好,网站管理员怎样管理moxy json介绍我们正在向EclipseLink MOXy添加从域模型生成JSON模式的 功能 。 为此#xff0c;我们创建了一个新的变量节点映射。 在本文中#xff0c;我将通过将Java模型映射到JSON模式来演示新的映射。 您可以使用每晚构建的EclipseLink 2.6.0进行尝试#xff1a; http… 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对象的集合。 而不是集合的默认表示形式请参阅 绑定到JSONXML –处理集合 我们希望每个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;XmlElementWrapperXmlVariableNode(name)public ListProperty properties;private ListString required;} JsonSchema存储在地图中的属性 在此版本的JsonSchema类中我们将属性的类型从List Property属性更改为Map StringProperty 。 注释保持不变所不同的是当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;XmlElementWrapperXmlVariableNode(name)public MapString, Property properties;private ListString 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 {MapString, Object properties new HashMapString, 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 version1.0?
xml-bindingsxmlnshttp://www.eclipse.org/eclipselink/xsds/persistence/oxmpackage-nameblog.variablenode.jsonschemaxml-accessor-typeFIELDjava-typesjava-type nameJsonSchemajava-attributesxml-variable-node java-attributeproperties java-variable-attributenamexml-element-wrapper//xml-variable-node/java-attributes/java-typejava-type namePropertyjava-attributesxml-transient java-attributename//java-attributes/java-type/java-types
/xml-bindings 参考 来自MOXy的XmlVariableNode –来自我们的JCG合作伙伴 Blaise Doughan的JSON模式示例 位于Java XMLJSON Binding博客上。 翻译自: https://www.javacodegeeks.com/2013/06/moxys-xmlvariablenode-json-schema-example.htmlmoxy json介绍