JAXB和构造函数 [英] JAXB and constructors
问题描述
我开始学习JAXB,所以我的问题可能很蠢。现在我有类并且想生成XML Schema。在此指示后,我得到例外
I'm starting learning JAXB, so my question can be very silly. Now I have classes and want generate XML Schema. Going after this instruction I get exception
IllegalAnnotationExceptions ...没有非arg默认的
构造函数。
IllegalAnnotationExceptions ... does not have a no-arg default constructor.
是啊。我的类没有默认没有arg的构造函数。太容易了。我有包可见构造函数/最终方法和偏离课程与参数的类。我应该做什么 - 创建一些特定的momemto / builder类或者指定我的构造函数到JAXB(以什么方式?)?感谢。
Yeah. My classes haven't default no-arg constructors. It's too easy. I have classes with package visible constructors / final methods and off course with arguments. What shall I do - create some specific momemto/builder classes or specify my constructors to JAXB (in what way?) ? Thanks.
推荐答案
JAXB可以使用XML适配器支持这种情况。考虑你有没有零arg参数构造函数的以下对象:
JAXB can support this case using an XML Adapter. Consider you have the following object with no zero-arg constructor:
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;
}
}
您只需创建一个可映射此类的版本:
You simply need to create a mappable version of this class:
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;
}
}
<他们:
And an XML Adapter to convert between them:
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注释:
Then for properties that refer to the Customer class, simply use the @XmlJavaTypeAdapter annotation:
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;
}
}
示例参见:
- http://bdoughan.blogspot.com/2010/12/jaxb-and-immutable-objects.html
这篇关于JAXB和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!