java - JAXB, how to validate nillable and required field when unmarshalling -
i have small problem jaxb, unfortunately not able find answer.
i have class customer, 2 fields name , city, mapping done using annotations , both fields marked required , not 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); } }
however, when submit such xml file:
<customer> <city>unknown</city> </customer>
i receive customer instance both fields set null.
shouldn't there validation exception or missing in mapping?
to unmarshal use:
jaxbcontext jaxbcontext = jaxbcontext.newinstance(customer.class); unmarshaller unmarshaller = jaxbcontext.createunmarshaller(); customer customer = (customer) unmarshaller.unmarshal(in);
you need use schema validate. jaxb can't validation on own.
schemafactory sf = schemafactory.newinstance(javax.xml.xmlconstants.w3c_xml_schema_ns_uri); schema schema = sf.newschema(classutils.getdefaultclassloader().getresource(schemapath)); unmarshaller.setschema(schema);
Comments
Post a Comment