注意: 我是 Eclipselink
JAXB(MOXy)的 负责人,并且是
JAXB(JSR-222) 专家组的成员。
如果您正在使用MOXy,则以下内容将起作用,并且应与任何其他JAXB提供程序一起使用。这种方法转换
java.util.Map成
org.w3c.dom.Element使用
XmlAdapter。
MapAdapter
An
XmlAdapter允许您将一个类的实例封送为另一个类的实例(请参阅:http : //blog.bdoughan.com/2010/07/xmladapter-jaxbs-
secret-weapon.html)。
package forum11353790;import java.util.*;import java.util.Map.Entry;import javax.xml.bind.annotation.adapters.XmlAdapter;import javax.xml.parsers.*;import org.w3c.dom.*;public class MapAdapter extends XmlAdapter<Element, Map<String, String>> { private documentBuilder documentBuilder; public MapAdapter() throws Exception { documentBuilder = documentBuilderFactory.newInstance().newdocumentBuilder(); } @Override public Element marshal(Map<String, String> map) throws Exception { document document = documentBuilder.newdocument(); Element rootElement = document.createElement("data"); document.appendChild(rootElement); for(Entry<String,String> entry : map.entrySet()) { Element childElement = document.createElement(entry.getKey()); childElement.setTextContent(entry.getValue()); rootElement.appendChild(childElement); } return rootElement; } @Override public Map<String, String> unmarshal(Element rootElement) throws Exception { NodeList nodeList = rootElement.getChildNodes(); Map<String,String> map = new HashMap<String, String>(nodeList.getLength()); for(int x=0; x<nodeList.getLength(); x++) { Node node = nodeList.item(x); if(node.getNodeType() == Node.ELEMENT_NODE) { map.put(node.getNodeName(), node.getTextContent()); } } return map; }}
人
您指定一个字段/属性应
XmlAdapter通过
@XmlJavaTypeAdapter注释利用。
package forum11353790;import java.io.Serializable;import java.util.*;import javax.xml.bind.annotation.*;import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;@XmlAccessorType( XmlAccessType.FIELD )@XmlRootElementpublic class Person implements Serializable { private String firstname; private String lastname; private List< Address > addresses = new ArrayList< Address >(); @XmlAnyElement @XmlJavaTypeAdapter(MapAdapter.class) private Map< String, String > data = new HashMap< String, String >();}
地址
package forum11353790;import java.io.Serializable;import javax.xml.bind.annotation.*;@XmlAccessorType(XmlAccessType.FIELD)public class Address implements Serializable { private String street; private String city; private String state; private String country; private String postalpre; private Integer addresstype;}
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要
jaxb.properties在与域模型相同的程序包中包含一个名为的文件,并包含以下条目(请参阅:http : //blog.bdoughan.com/2011/05/specifying-
eclipselink-moxy-as -your.html)。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示版
下面是一个独立的示例,您可以运行它来证明一切正常。
package forum11353790;import java.io.FileInputStream;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>(2); properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties); Unmarshaller unmarshaller = jc.createUnmarshaller(); StreamSource json = new StreamSource(new FileInputStream("src/forum11353790/input.json")); Person person = unmarshaller.unmarshal(json, Person.class).getValue(); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, System.out); }}
input.json /输出
{ "firstname" : "Jimmy", "lastname" : "Johns", "addresses" : [ { "street" : "19 Mayberry Drive", "city" : "Mayberry", "state" : "nc", "country" : "us", "postalpre" : "27043", "addresstype" : 1 } ], "data" : { "sandwich" : "roast beef", "hair" : "brown", "eyes" : "blue" }}
MOXy和JAX-RS / Jersey
您可以通过利用该类在JAX-RS环境中利用MOXy
MOXyJsonProvider:
- http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)