弹簧。在XML上的根元素之后添加子元素

时间:2018-03-15 06:34:34

标签: java xml spring marshalling

我有以下代码:

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "responseClientInfo")
public class SignUpStatus {

    @XmlElement(name = "clientCode")
    private Integer clientId;

    public Integer getClientId() {
        return clientId;
    }

    public void setClientId(Integer clientId) {
        this.clientId = clientId;
    }
}

我接下来的xml:

<responseClientInfo>
    <clientCode>120118</clientCode>
</responseClientInfo>

但我需要接下来:

<responseClientInfo>
  <clientInfo>
        <clientCode>120118</clientCode>
  </clientInfo>    
</responseClientInfo>

我知道可以将clientCode包装到类clientInfo中并将这些新类设置为xmlElement,但是可以以更清晰和优雅的方式进行吗?

1 个答案:

答案 0 :(得分:1)

尝试使用此XmlElementWrapper

@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "responseClientInfo")
public class SignUpStatus {

@XmlElement(name = "clientCode")
@XmlElementWrapper(name="clientInfo")
private Integer clientId;

public Integer getClientId() {
    return clientId;
}

public void setClientId(Integer clientId) {
    this.clientId = clientId;
}
}