你想要做什麼聽起來像一個可怕的黑客。
你的問題,如果我得到了正確的是,在你的行動中用作參數的對象是不可變的。幸運的是,有很多方法可以用註釋來定製JAXB映射。應該有可能讓你的類不可變,但使這些字段對JAXB可見。
從this answer,我看到:
package blog.immutable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="customer")
@XmlAccessorType(XmlAccessType.NONE)
public final class Customer {
@XmlAttribute
private final String name;
@XmlElement
private final Address address;
@SuppressWarnings("unused")
private Customer() {
this(null, null);
}
public Customer(String name, Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
}
如果你不喜歡上面的代碼需要一個無參數的構造Customer()
的事實,你可以看看這更complicated approach。
你在寫問題時應該多加小心。 – ewernli 2013-05-06 07:08:07