java - Issue with the blank non mandatory date field in JAXB in SOAP UI -
i working on financial sector project in jaxb technology. have developed web service consumption part , got stuck issue been reported client end. have 2 fields in bean class of date type follows
@xmlelement(name = "abc") @xmlschematype(name = "date") private date abc; @xmlelement(name = "qwe") @xmlschematype(name = "date") private date qwe; now when these fields not have data gives exception these fields not mandatory, takes date in yyyy-mm-dd per gregorian calender per annotation tags. reqest when done testing @ local end using soap ui
</data> <!--optional:--> <abc></qwe> <!--optional:--> <qwe></qwe> </data> exception description: object [], of class [class java.lang.string], mapping [org.eclipse.persistence.oxm.mappings.xmldirectmapping[abc-->abc/text()]] descriptor [xmldescriptor(fully qualified package name --> [])], not converted [class java.util.calendar]. same case both of these fields , works when date filled in expected format.
i donot know how handle issue either through tags or other means gives exception @ bean class not reaches service layer.
any suggestion solve problem appreciable.
thanks.
base problem
empty string not valid value xsd:date type. valid xml schema optional element should represented absent node.,
why base problem impacting you
all jaxb implementations recognize empty string not valid value xsd:date. reporting instance of validationeventhandler. can see doing following:
unmarshaller unmarshaller = jc.createunmarshaller(); unmarshaller.seteventhandler(new validationeventhandler() { @override public boolean handleevent(validationevent event) { system.out.println(event); return true; } }); the implementation of jax-ws using, leverages eclipselink moxy jaxb provider. , in version using moxy default throw exception when validationevent of severity error encountered instead of fatal_error reference implementation. has since been fixed in following bug:
work around
if using jaxb apis directly override default validationeventhandler. in jax-ws environment xmladapter can used provide custom conversion logic. leverage xmladapter override how conversion to/from date handled.
xmladapter (dateadapter)
import java.text.simpledateformat; import java.util.date; import javax.xml.bind.annotation.adapters.xmladapter; public class dateadapter extends xmladapter<string, date>{ private simpledateformat dateformat = new simpledateformat("yyyy-mm-dd"); @override public date unmarshal(string v) throws exception { if(v.length() == 0) { return null; } return dateformat.parse(v); } @override public string marshal(date v) throws exception { if(null == v) { return null; } return dateformat.format(v); } } java model (root)
the xmladapter referenced using @xmljavatypeadapter annotation. if wish xmladapter apply instances of date can register @ package level (see: http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html).
import java.util.date; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.xmljavatypeadapter; @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public class root { @xmlschematype(name = "date") @xmljavatypeadapter(value=dateadapter.class, type=date.class) private date abc; @xmlschematype(name="date") @xmljavatypeadapter(value=dateadapter.class, type=date.class) private date qwe; } demo code
below standalone example can run see works.
jaxb.properties
in standalone example use moxy jaxb provider need include file called jaxb.propeties in same package domain model following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.jaxbcontextfactory input.xml
<?xml version="1.0" encoding="utf-8"?> <root> <abc></abc> <qwe>2013-09-05</qwe> </root> demo
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(root.class); unmarshaller unmarshaller = jc.createunmarshaller(); file xml = new file("src/forum18617998/input.xml"); root root = (root) unmarshaller.unmarshal(xml); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(root, system.out); } } output
note in marshalled xml date field null marshalled absent element (see: http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html).
<?xml version="1.0" encoding="utf-8"?> <root> <qwe>2013-09-05</qwe> </root>
Comments
Post a Comment