JAXB杰克逊 - 如何处理地图?

时间:2012-12-09 02:43:08

标签: rest jaxb jackson

我有一个Web应用程序,我在其中从域对象生成POJO。我的一个域对象包含一个映射,JAXB生成以下模式:

<xs:element name="persons">
  <xs:complexType>
      <xs:sequence>
          <xs:element name="entry" minOccurs="0" maxOccurs="unbounded">
              <xs:complexType>
                 <xs:sequence>
                    <xs:element name="key" minOccurs="0" type="xs:string"/>
                    <xs:element name="value" minOccurs="0" type="person"/>
                 </xs:sequence>
              </xs:complexType>
          </xs:element>
      </xs:sequence>
   </xs:complexType>
</xs:element>

这是由HashMap<String, Person>人生成的:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "personConfiguration", propOrder = {
    "persons",
})
@XmlRootElement(name = "personConfiguration")
public class PersonConfiguration
{

    @XmlElement(required = true)
    protected PersonConfiguration.Persons persons;

    /**
     * Gets the value of the persons property.
     * 
     * @return
     *     possible object is
     *     {@link PersonConfiguration.Persons }
     *     
     */
    public PersonConfiguration.Persons getPersons() {
        return persons;
    }

    /**
     * Sets the value of the persons property.
     * 
     * @param value
     *     allowed object is
     *     {@link PersonConfiguration.Persons }
     *     
     */
    public void setPersons(PersonConfiguration.Persons value) {
        this.persons = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "entry"
    })
    public static class Persons
    {

        protected List<PersonConfiguration.Persons.Entry> entry;

        /**
         * Gets the value of the entry property.
         * 
         * <p>
         * This accessor method returns a reference to the live list,
         * not a snapshot. Therefore any modification you make to the
         * returned list will be present inside the JAXB object.
         * This is why there is not a <CODE>set</CODE> method for the entry property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getEntry().add(newItem);
         * </pre>
         * 
         * 
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link PersonConfiguration.Persons.Entry }
         * 
         * 
         */
        public List<PersonConfiguration.Persons.Entry> getEntry() {
            if (entry == null) {
                entry = new ArrayList<PersonConfiguration.Persons.Entry>();
            }
            return this.entry;
        }


        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "", propOrder = {
            "key",
            "value"
        })
        public static class Entry
        {

            protected String key;
            protected Person value;

            /**
             * Gets the value of the key property.
             * 
             * @return
             *     possible object is
             *     {@link String }
             *     
             */
            public String getKey() {
                return key;
            }

            /**
             * Sets the value of the key property.
             * 
             * @param value
             *     allowed object is
             *     {@link String }
             *     
             */
            public void setKey(String value) {
                this.key = value;
            }

            /**
             * Gets the value of the value property.
             * 
             * @return
             *     possible object is
             *     {@link Person }
             *     
             */
            public Person getValue() {
                return value;
            }

            /**
             * Sets the value of the value property.
             * 
             * @param value
             *     allowed object is
             *     {@link Person }
             *     
             */
            public void setValue(Person value) {
                this.value = value;
            }

        }

    }

}

正如可以看到JAXB添加了这个额外级别的间接条目 - &gt;核心价值。其他部分是Spring MVC,使用JSON对象进行REST调用。

现在基于XML的REST调用可以正常使用上面的对象模式,但是当使用具有相同模式的JSON消息发送相同的调用时,我得到JSONMappingException。

为什么会发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:1)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

使用不同的XML和JSON绑定提供程序时,很难保持XML和JSON表示的一致性。以下是使用MOXy作为XML和JSON提供程序,并将所有映射信息作为JAXB注释提供的简化示例。

<强>根

下面是一个示例域对象,其中persons字段将根据您的问题生成XML架构片段。

package forum13784163;

import java.util.Map;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    Map<String, Person> persons;

}

<强>人

以下是您的Person课程的样子。请注意我如何将id字段映射到XML属性。

package forum13784163;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Person {

    @XmlAttribute
    int id;

    String name;

    int age;

}

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

<强> input.json

如果将MOXy用作JSON绑定提供程序,则下面是JSON表示形式。

{
   "persons" : {
      "entry" : [ {
         "key" : "Jane",
         "value" : {
            "id" : 123,
            "name" : "Jane",
            "age" : 30
         }
      } ]
   }
}

<强>演示

在下面的演示代码中,JSON被解组到对象中,然后这些相同的对象被编组为XML。这是从一个包含一组元数据的JAXBContext完成的。

package forum13784163;

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.UnmarshallerProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
        unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
        StreamSource json = new StreamSource("src/forum13784163/input.json");
        Root root = unmarshaller.unmarshal(json, Root.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

<强>输出

以下是生成的XML。

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <persons>
      <entry>
         <key>Jane</key>
         <value id="123">
            <name>Jane</name>
            <age>30</age>
         </value>
      </entry>
   </persons>
</root>

了解更多信息