java - JaxB binding XMLGregorianCalendar -
java - JaxB binding XMLGregorianCalendar -
jaxb when binding improper date format xmlgregoriancalendar not throwing exception. instead assigns null. problem?
@xmlattribute(name = "travelenddate", required = true) @xmlschematype(name = "date") protected xmlgregoriancalendar travelenddate; <xs:complextype name="searchcriteria"> <xs:attribute name="travelstartdate" type="xs:date" use="required"/> <xs:attribute name="travelenddate" type="xs:date" use="required"/> </xs:complextype>
note: i'm eclipselink jaxb (moxy) lead, , fellow member of jaxb 2.x (jsr-222) expert group.
this behaviour vary little between jaxb implementations. moxy illustration throw next exception if value incorrect:
exception in thread "main" javax.xml.bind.unmarshalexception - linked exception: [exception [eclipselink-3003] (eclipse persistence services - 2.4.0.qualifier): org.eclipse.persistence.exceptions.conversionexception exception description: wrong date format: [2011-02-50] (expected [yyyy-mm-dd])] @ org.eclipse.persistence.jaxb.jaxbunmarshaller.unmarshal(jaxbunmarshaller.java:225) @ forum254.demo.main(demo.java:18) caused by: exception [eclipselink-3003] (eclipse persistence services - 2.4.0.qualifier): org.eclipse.persistence.exceptions.conversionexception exception description: wrong date format: [2011-02-50] (expected [yyyy-mm-dd]) @ org.eclipse.persistence.exceptions.conversionexception.incorrectdateformat(conversionexception.java:103) @ org.eclipse.persistence.internal.oxm.xmlconversionmanager.convertstringtoxmlgregoriancalendar(xmlconversionmanager.java:689) @ org.eclipse.persistence.internal.oxm.xmlconversionmanager.convertobjecttoxmlgregoriancalendar(xmlconversionmanager.java:278) @ org.eclipse.persistence.internal.oxm.xmlconversionmanager.convertobject(xmlconversionmanager.java:249) @ org.eclipse.persistence.oxm.xmlfield.convertvaluebasedonschematype(xmlfield.java:712)
workaround
to error reported on jaxb implementation can set xml schema on unmarshaller perform validation:
http://bdoughan.blogspot.com/2010/12/jaxb-and-marshalunmarshal-schema.htmlor can implement xmladapter
command conversion (and throw exception if necessary) yourself:
java binding jaxb
Comments
Post a Comment