Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB将复杂对象映射到JSON_Java_Json_Jaxb_Moxy - Fatal编程技术网

Java JAXB将复杂对象映射到JSON

Java JAXB将复杂对象映射到JSON,java,json,jaxb,moxy,Java,Json,Jaxb,Moxy,我的复杂对象包含另一个复杂对象的映射,序列化为JSON时遇到问题。我正在使用JAXB和MOXy。以下是我的课程: 要序列化/反序列化的根类: @XmlRootElement @XmlSeeAlso(ComplexBean.class) @XmlAccessorType(XmlAccessType.FIELD) public class MapBean implements Serializable { @XmlJavaTypeAdapter(MapAdapter.class)

我的复杂对象包含另一个复杂对象的映射,序列化为JSON时遇到问题。我正在使用JAXB和MOXy。以下是我的课程: 要序列化/反序列化的根类:

@XmlRootElement
@XmlSeeAlso(ComplexBean.class)
@XmlAccessorType(XmlAccessType.FIELD)
public class MapBean implements Serializable {

    @XmlJavaTypeAdapter(MapAdapter.class)
    @XmlElement
    private Map<String, ComplexBean> complexBeans;
public class ComplexBean implements Serializable {
    private String name;
    private String surName;
    private double weight;
public class MapAdapter extends XmlAdapter<Element, Map<String, ComplexBean>> {

private DocumentBuilder documentBuilder;

public MapAdapter() throws Exception {
    documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}

public Element marshal(Map<String, ComplexBean> map) throws Exception {
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement("complexBeans");
    document.appendChild(rootElement);
    for(Map.Entry<String, ComplexBean> entry : map.entrySet()) {
        Element childElement = document.createElement(entry.getKey());
        ComplexBean complexBean = entry.getValue();

        Attr nameAttribute = document.createAttribute("name");
        nameAttribute.setValue(complexBean.getName());
        childElement.setAttributeNode(nameAttribute);

        Attr surNameAttribute = document.createAttribute("surName");
        surNameAttribute.setValue(complexBean.getSurName());
        childElement.setAttributeNode(surNameAttribute);

        Attr weightAttribute = document.createAttribute("weight");
        weightAttribute.setValue(String.valueOf(complexBean.getWeight()));
        childElement.setAttributeNode(weightAttribute);

        rootElement.appendChild(childElement);
    }
    return rootElement;
}

public Map<String, ComplexBean> unmarshal(Element element) throws Exception {
    System.out.println("In unmarshaller");
    Map<String, ComplexBean> complexBeans = new HashMap<String, ComplexBean>();
    return null;
}
}
public static void main(String[] args) throws Exception {
    Map<String, Object> jaxbProperties = new HashMap<String, Object>();
    jaxbProperties.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
    jaxbProperties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(new Class[] {MapBean.class}, jaxbProperties);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    MapBean mapBean = getMapBean();

    Writer writer = new StringWriter();
    marshaller.marshal(mapBean, writer);
    String json = writer.toString();
    System.out.println("Output JSON: ");
    System.out.println(json);

    StreamSource streamSource = new StreamSource(json);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    MapBean unmarshalledMapBean = unmarshaller.unmarshal(streamSource, MapBean.class).getValue();
    System.out.println(unmarshalledMapBean);
}
MapAdapter类:

@XmlRootElement
@XmlSeeAlso(ComplexBean.class)
@XmlAccessorType(XmlAccessType.FIELD)
public class MapBean implements Serializable {

    @XmlJavaTypeAdapter(MapAdapter.class)
    @XmlElement
    private Map<String, ComplexBean> complexBeans;
public class ComplexBean implements Serializable {
    private String name;
    private String surName;
    private double weight;
public class MapAdapter extends XmlAdapter<Element, Map<String, ComplexBean>> {

private DocumentBuilder documentBuilder;

public MapAdapter() throws Exception {
    documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
}

public Element marshal(Map<String, ComplexBean> map) throws Exception {
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement("complexBeans");
    document.appendChild(rootElement);
    for(Map.Entry<String, ComplexBean> entry : map.entrySet()) {
        Element childElement = document.createElement(entry.getKey());
        ComplexBean complexBean = entry.getValue();

        Attr nameAttribute = document.createAttribute("name");
        nameAttribute.setValue(complexBean.getName());
        childElement.setAttributeNode(nameAttribute);

        Attr surNameAttribute = document.createAttribute("surName");
        surNameAttribute.setValue(complexBean.getSurName());
        childElement.setAttributeNode(surNameAttribute);

        Attr weightAttribute = document.createAttribute("weight");
        weightAttribute.setValue(String.valueOf(complexBean.getWeight()));
        childElement.setAttributeNode(weightAttribute);

        rootElement.appendChild(childElement);
    }
    return rootElement;
}

public Map<String, ComplexBean> unmarshal(Element element) throws Exception {
    System.out.println("In unmarshaller");
    Map<String, ComplexBean> complexBeans = new HashMap<String, ComplexBean>();
    return null;
}
}
public static void main(String[] args) throws Exception {
    Map<String, Object> jaxbProperties = new HashMap<String, Object>();
    jaxbProperties.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
    jaxbProperties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(new Class[] {MapBean.class}, jaxbProperties);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    MapBean mapBean = getMapBean();

    Writer writer = new StringWriter();
    marshaller.marshal(mapBean, writer);
    String json = writer.toString();
    System.out.println("Output JSON: ");
    System.out.println(json);

    StreamSource streamSource = new StreamSource(json);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    MapBean unmarshalledMapBean = unmarshaller.unmarshal(streamSource, MapBean.class).getValue();
    System.out.println(unmarshalledMapBean);
}
但是umarshaller没有调用MapAdapter类中的unmarshall方法,并且失败,出现异常:

Caused by: Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: java.net.MalformedURLException: no protocol: {
   "complexBeans" : {
      "first" : {
         "name" : "cbName1",
         "surName" : "cbSurName1",
         "weight" : "50.5"
      },
      "second" : {
         "name" : "cbName2",
         "surName" : "cbSurName2",
         "weight" : "10.5"
      }
   }
}
    at  org.eclipse.persistence.exceptions.XMLMarshalException.unmarshalException(XMLMarshalException.java:120)
    at org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:156)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:863)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:710)
    at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:643)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:339)
    ... 1 more
    at java.net.URL.<init>(URL.java:586)
    at java.net.URL.<init>(URL.java:483)
    at java.net.URL.<init>(URL.java:432)
    at   org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:127)
    at org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:154)
    ... 5 more
原因:异常[EclipseLink-25004](Eclipse持久性服务-2.6.1.v20150916-55dc7c3):org.Eclipse.Persistence.exceptions.xmlMarshallException
异常说明:解组文档时出错
内部异常:java.net.MalformedURLException:无协议:{
“complexBeans”:{
“第一”:{
“名称”:“cbName1”,
“姓氏”:“cbSurName1”,
“重量”:“50.5”
},
“第二”:{
“名称”:“cbName2”,
“姓氏”:“cbSurName2”,
“重量”:“10.5”
}
}
}
位于org.eclipse.persistence.exceptions.xmlmarshaleexception.unmarshaleexception(xmlmarshaleexception.java:120)
位于org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:156)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:863)
位于org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:710)
位于org.eclipse.persistence.internal.oxm.xmlumarshaller.unmarshal(xmlumarshaller.java:643)
位于org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:339)
... 还有一个
在java.net.URL.(URL.java:586)
在java.net.URL.(URL.java:483)
位于java.net.URL。(URL.java:432)
位于org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:127)
位于org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:154)
... 还有5个

所以,请帮助我将json解组到MapBean对象。我的错误是什么?

您的
StreamSource
调用在main方法中是错误的。您直接将json字符串传递给StreamSource构造函数

这是您目前拥有的--

您应该有如下内容,根据


非常感谢你!你救了我一天。我甚至不能认为我以错误的方式创建StreamSource。我已经用不同的MapAdapter整理了这么多选项,但错误完全不同!