java - MOXy - Automatically unmarshal correct class from a list of classes -
is possible make unmarshaller select correct class deserialize list of classes? moxy jaxbcontext (it correct one, double checked this) created both classes referenced, everytime unmarshaller creates first class mentioned. regardless content provide. see example files further information.
the xml content loaded xml api (the eve online xml api if information required), response might error or valid response object. structure of responses looks this:
error response
<?xml version='1.0' encoding='utf-8'?> <eveapi version="2"> <currenttime>2015-07-20 21:13:14</currenttime> <error code="106">must provide userid or keyid parameter authentication.</error> <cacheduntil>2015-07-21 09:13:14</cacheduntil> </eveapi>
valid response
<?xml version='1.0' encoding='utf-8'?> <eveapi version="2"> <currenttime>2015-07-19 14:58:36</currenttime> <result> <serveropen>true</serveropen> <onlineplayers>28033</onlineplayers> </result> <cacheduntil>2015-07-19 14:59:36</cacheduntil> </eveapi>
there no schema available, had create mapping classes scratch. use inheritance structure since responses contain similar base elements. use project lombok create getter, setter , such methods.
eveapienvelope (base class responses)
@getter @equalsandhashcode @tostring @xmlaccessortype(xmlaccesstype.field) public abstract class eveapienvelope { @xmlattribute(name = "version") protected int version; @xmlelement(name = "currenttime") @xmljavatypeadapter(zoneddatetimeadapter.class) protected zoneddatetime currenttime; @xmlelement(name = "cacheduntil") @xmljavatypeadapter(zoneddatetimeadapter.class) protected zoneddatetime cacheduntil; }
eveapierror (mapping class error response)
@getter @equalsandhashcode(callsuper = true) @tostring(callsuper = true) @xmlrootelement(name = "eveapi") @xmlaccessortype(xmlaccesstype.field) public class eveapierror extends eveapienvelope { @xmlelement(name = "error") private errorresult error; @getter @equalsandhashcode @tostring @xmlaccessortype(xmlaccesstype.field) public static class errorresult { @xmlattribute(name = "code") private int code; @xmlvalue private string message; } }
serverstatusresult (mapping class valid response)
@getter @equalsandhashcode(callsuper = true) @tostring(callsuper = true) @xmlrootelement(name = "eveapi") @xmlaccessortype(xmlaccesstype.field) public class serverstatusresult extends eveapienvelope { @xmlpath("result/serveropen/text()") private boolean serveropen; @xmlpath("result/onlineplayers/text()") private long onlineplayers; }
now when use these classes create jaxbcontext, regardless of input provide, result same. class declare first in context creation.
inputstream xmlsource = this.getclass().getclassloader().getresourceasstream("xml/error.xml"); jaxbcontext jc = jaxbcontext.newinstance(serverstatusresult.class, eveapierror.class); unmarshaller um = jc.createunmarshaller(); // 'result' of type serverstatusresult, regardless of input object result = um.unmarshal(xmlsource);
inputstream xmlsource = this.getclass().getclassloader().getresourceasstream("xml/serverstatus.xml"); jaxbcontext jc = jaxbcontext.newinstance(eveapierror.class, serverstatusresult.class); unmarshaller um = jc.createunmarshaller(); // 'result' of type eveapierror, regardless of input object result = um.unmarshal(xmlsource);
what point in providing multiple classes jaxbcontext anyway, if first 1 selected if not match xml structure?
how can make moxy select eveapierror class if <error>
element present , other provided class otherwise? possible @ all?
thanks in advance. if need further information please drop comment.
Comments
Post a Comment