xml - MOXy ignores XmlElement(required=true) -
i have following pojos:
@xmlrootelement(name = "root") @xmlaccessortype(xmlaccesstype.field) public class datalist<t> { @xmlelementwrapper(name="resulttablesection") @xmlanyelement(lax = true) public list<t> elements; } @xmlrootelement(name = "resulttable") @xmlaccessortype(xmlaccesstype.field) public class event { @xmlelement(name = "tsseventid", required = true) public integer id; ... }
when in response xml tsseventid tag missing datalist in event.id field null regardless of set required=true. nothing fails. expected behavior ? how can enable validation ?
unmarshall code:
jaxbcontext.newinstance(datalist.class, hall.class).createunmarshaller().unmarshal(xmldocument);
dependency:
<dependency> <groupid>org.eclipse.persistence</groupid> <artifactid>org.eclipse.persistence.moxy</artifactid> <version>2.5.1</version> </dependency>
a jaxb (jsr-222) implementation not expected throw exception or validationevent
if required element missing. required
flag on @xmlelement
impacts how xml schema generated.
demo
in demo code below have specified validationeventhandler
on unmarshaller
catch exceptions. running code either moxy or jaxb reference implementation won't result in exceptions being thrown.
import java.io.file; import javax.xml.bind.*; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(datalist.class, event.class); unmarshaller unmarshaller = jc.createunmarshaller(); unmarshaller.seteventhandler(new validationeventhandler() { @override public boolean handleevent(validationevent event) { system.out.println(event.getmessage()); return false; } }); file xml = new file("src/forum21593351/input.xml"); datalist dl = (datalist) unmarshaller.unmarshal(xml); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(dl, system.out); } }
input.xml/output
<?xml version="1.0" encoding="utf-8"?> <root> <resulttablesection> <resulttable> </resulttable> </resulttablesection> </root>
enabling validation
how can enable validation ?
you can set instance of schema
on unmarshaller
have input validated.
Comments
Post a Comment