注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。
如果您使用 MOXy,以下内容将起作用,并且应该与任何其他 JAXB 提供程序一起使用。这种方法使用将 转换java.util.Map
为。org.w3c.dom.Element
XmlAdapter
地图适配器
AnXmlAdapter
允许您将一个类的实例编组为另一个类的实例(参见: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
via@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 )
@XmlRootElement
public 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 postalcode;
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);
}
}
输入.json/输出
{
"firstname" : "Jimmy",
"lastname" : "Johns",
"addresses" : [ {
"street" : "19 Mayberry Drive",
"city" : "Mayberry",
"state" : "nc",
"country" : "us",
"postalcode" : "27043",
"addresstype" : 1
} ],
"data" : {
"sandwich" : "roast beef",
"hair" : "brown",
"eyes" : "blue"
}
}
MOXy 和 JAX-RS/Jersey
您可以通过利用该类在 JAX-RS 环境中利用 MOXy MOXyJsonProvider
: