JAXB 和构造函数
2022-09-01 12:41:47
我开始学习JAXB,所以我的问题可能非常愚蠢。现在我有类,想要生成XML架构。按照这个指令,我得到异常
非法注释例外情况 ...没有无参数默认构造函数。
是的。我的类没有默认的无参数构造函数。这太容易了。我有带有包可见构造函数/最终方法的类,并且带有参数。我该怎么办 - 创建一些特定的mometo/构建器类或将我的构造函数指定给JAXB(以什么方式?)?谢谢。
我开始学习JAXB,所以我的问题可能非常愚蠢。现在我有类,想要生成XML架构。按照这个指令,我得到异常
非法注释例外情况 ...没有无参数默认构造函数。
是的。我的类没有默认的无参数构造函数。这太容易了。我有带有包可见构造函数/最终方法的类,并且带有参数。我该怎么办 - 创建一些特定的mometo/构建器类或将我的构造函数指定给JAXB(以什么方式?)?谢谢。
JAXB 可以使用 XML 适配器支持这种情况。假设您有以下没有零 arg 构造函数的对象:
package blog.immutable;
public class Customer {
private final String name;
private final Address address;
public Customer(String name, Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
}
您只需要创建此类的可映射版本:
package blog.immutable.adpater;
import javax.xml.bind.annotation.XmlAttribute;
import blog.immutable.Address;
public class AdaptedCustomer {
private String name;
private Address address;
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
以及要在它们之间进行转换的 XML 适配器:
package blog.immutable.adpater;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import blog.immutable.Customer;
public class CustomerAdapter extends XmlAdapter<AdaptedCustomer, Customer> {
@Override
public Customer unmarshal(AdaptedCustomer adaptedCustomer) throws Exception {
return new Customer(adaptedCustomer.getName(), adaptedCustomer.getAddress());
}
@Override
public AdaptedCustomer marshal(Customer customer) throws Exception {
AdaptedCustomer adaptedCustomer = new AdaptedCustomer();
adaptedCustomer.setName(customer.getName());
adaptedCustomer.setAddress(customer.getAddress());
return adaptedCustomer;
}
}
然后,对于引用 Customer 类的属性,只需使用@XmlJavaTypeAdapter注释:
package blog.immutable;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import blog.immutable.adpater.CustomerAdapter;
@XmlRootElement(name="purchase-order")
public class PurchaseOrder {
private Customer customer;
@XmlJavaTypeAdapter(CustomerAdapter.class)
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
有关更详细的示例,请参阅:
您可以使用注释并以各种组合使用 factoryMethod / factoryClass 属性,例如:@XmlType
@XmlType(factoryMethod="newInstance")
@XmlRootElement
public class PurchaseOrder {
@XmlElement
private final String address;
@XmlElement
private final Customer customer;
public PurchaseOrder(String address, Customer customer){
this.address = address;
this.customer = customer;
}
private PurchaseOrder(){
this.address = null;
this.customer = null;
}
/** Creates a new instance, will only be used by Jaxb. */
private static PurchaseOrder newInstance() {
return new PurchaseOrder();
}
public String getAddress() {
return address;
}
public Customer getCustomer() {
return customer;
}
}
令人惊讶的是,这是有效的,并且在取消编组时您将获得初始化的实例。您应该注意不要在代码上的任何位置调用该方法,因为它将返回无效的实例。newInstance