我对JAXB有一个小问题,但不幸的是我无法找到答案.
我有一个Customer类,有2个字段名称和城市,映射是使用注释完成的,并且两个字段都标记为必需而不是nillable.
@XmlRootElement(name = "customer")
public class Customer {
enum City {
PARIS, LONDON, WARSAW
}
@XmlElement(name = "name", required = true, nillable = false)
public String name;
@XmlElement(name = "city", required = true, nillable = false)
public City city;
@Override
public String toString(){
return String.format("Name %s, city %s", name, city);
}
}
但是,当我提交这样的XML文件时:
UNKNOWN
我将收到一个Customer实例,其中两个字段都设置为null.
不应该有验证异常,或者我在映射中遗漏了什么?
为了解组我使用:
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(in);