1

在我的 XML 中,我只输出一个元素,如果它有一个值,但 JSON 的要求是输出值,即使没有设置

即XML

<alias-list>
<alias sort-name="Afghan">Afghany</alias>
</alias-list>

我希望 JSON 输出别名元素的其他元素,即使未设置

IE

   "aliases" : [ {
      sort-name : "Afghan"
      begin-date : null
      end-date : null
      value : "Afghany"
   } ]

但我所能做的就是在输出 json 时使用一种方法将这些空元素设置为空字符串,这给了我

   "aliases" : [ {
      sort-name : "Afghan"
      begin-date : ""
      end-date : ""
      value : "Afghany"
   } ]

但这不是我想要的

更新尝试使用丹尼斯答案

我在建议的解决方案中遇到了一些问题,首先这里是完整的 Alias 类,我认为这会有所帮助(注意使用 JAXB 自动生成的类,我在 ElipseLink 之前遇到过这个问题,因为我的 Xml 输出还可以,我不是特别热衷改变这一点)

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-792 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2013.06.04 at 02:00:21 PM BST 
//


package org.musicbrainz.mmd2;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;attribute name="locale" type="{http://musicbrainz.org/ns/mmd-2.0#}def_iso-3166-2-code" />
 *       &lt;attribute name="sort-name" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" />
 *       &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" />
 *       &lt;attribute name="primary" type="{http://www.w3.org/2001/XMLSchema}anySimpleType" />
 *       &lt;attribute name="begin-date" type="{http://musicbrainz.org/ns/mmd-2.0#}def_incomplete-date" />
 *       &lt;attribute name="end-date" type="{http://musicbrainz.org/ns/mmd-2.0#}def_incomplete-date" />
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "alias")
public class Alias {

    @XmlValue
    protected String content;
    @XmlAttribute
    protected String locale;
    @XmlAttribute(name = "sort-name")
    @XmlSchemaType(name = "anySimpleType")
    protected String sortName;
    @XmlAttribute
    @XmlSchemaType(name = "anySimpleType")
    protected String type;
    @XmlAttribute
    @XmlSchemaType(name = "anySimpleType")
    protected String primary;
    @XmlAttribute(name = "begin-date")
    protected String beginDate;
    @XmlAttribute(name = "end-date")
    protected String endDate;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}

当我尝试 json 绑定解决方案时,我得到

Exception Description: The property or field beginDate on the class org.musicbrainz.mmd2.Alias is required to be included in the propOrder element of the XmlType annotation.
 - with linked exception:
[Exception [EclipseLink-50013] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The property or field beginDate on the class org.musicbrainz.mmd2.Alias is required to be included in the propOrder element of the XmlType annotation.]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1021)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
    at org.musicbrainz.search.servlet.mmd2.ResultsWriter.initJsonContext(ResultsWriter.java:100)
    ... 25 more

所以我然后编辑 Alias.java 将 beginDate 添加到 propOrder,但这给出了这个错误。这是真的 - 内容是用 @XmlValue 注释的,但我不明白为什么这是一个问题。

Exception Description: The property or field beginDate must be an attribute because another field or property is annotated with XmlValue.]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1021)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:174)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:165)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:152)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:112)
    at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:102)
    at org.musicbrainz.search.servlet.mmd2.ResultsWriter.initJsonContext(ResultsWriter.java:100)
    ... 30 more

不幸的是,我不能使用 @XmlElement 选项,因为 beginDate 是一个属性而不是一个元素,并且已经有一个 @XmlAttribute 注释,这似乎不支持 nillable。

4

1 回答 1

1

@XmlElement 注释上有一个可空选项,您可以使用它来处理此问题。但是,如果设置了此项,那么您将在 JSON 中获得您想要的内容,但是当值为 null 时,XML 将添加一个 xsi:nil 属性。如果您想要 JSON 和 XML 之间的不同行为,您可以使用绑定文件而不是在对象上添加注释。然后,您将创建 2 个具有不同绑定文件的 JAXBContext(或一个具有绑定文件而一个没有)以指定不同的行为。

使用有关 Alias.java 的更新信息,您还需要清除 prop-order(或者您可以根据需要列出所有元素和顺序,我刚刚在下面的 oxm.xml 示例中清除了它)。此外,由于内容在 oxm.xml 中有一个用于 JSON 绑定的 @XmlValue 注释,因此可以将其更改为作为常规元素处理(并将其命名为 value 或任何您想要调用的名称)。

注释示例

public class Alias{

  @XmlElement(name="begin-date", nillable= true)
  public String beginDate;
}

绑定文件示例 (oxm.xml)

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="mypackage.test">
    <java-types>
       <java-type name="Alias">
          <xml-type prop-order=""/>
          <java-attributes>
             <xml-element java-attribute="beginDate" name="begin-date" nillable="true"/>
             <xml-element java-attribute="content" name="value"/>
          </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

要使用绑定文件创建 JAXBContext,请执行以下操作

Map<String, Object> props = new HashMap<String, Object>();
StreamSource ss = new StreamSource(new File("pathtobindings/oxm.xml"));
props.put(JAXBContextProperties.OXM_METADATA_SOURCE, ss);
JAXBContext contextWithBindings = JAXBContext.newInstance(myClasses, props);
于 2013-06-05T15:21:00.523 回答