Десериализация XML в POJO с использованием XmlMapper
У меня есть следующий XML, который я хотел бы десериализовать:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<Job id="aut_dup" name="duplicateur">
<params>
<param id="instance" name="Nom de l'instance" mandatory="yes" desc="nom de l'instance dupliquée"/>
<param id="date" name="Date" desc="date du plan de l'instance dupliquée" format="YYYYMMJJ" default="date du plan du job courant"/>
<param id="server" name="Serveur de production" desc="nom du serveur de production principal de l'instance dupliquée" default="localhost"/>
<param id="port" name="Port" desc="port du serveur de production principal de l'instance dupliquée" default="port du serveur de production du job courant"/>
<param id="starttime" name="Heure de démarrage" desc="heure de démarrage de l'instance dupliquée" format="hh:mm" default="00:00"/>
<param id="endtime" name="Heure de fin" desc="heure de fin de l'instance dupliquée" format="jj/hh:mm" default="99/23:59"/>
<param id="lres" name="Ressources locales" desc="modification des ressources locales à l'instance dupliquée" format="ResNom1=valeur1,ResNom2=valeur2, ... Pour les FIFO: ResNom1=valeur11#valeur12#valeur13,ResNom2=...)"/>
<param id="sendmode" name="Mode" desc="0: mode non sécurisé, envoi en UDP. 1: mode sécurisé, envoi en TCP" format="0 ou 1" default="1"/>
<param id="retry" name="Réessais" desc="nombre de réessais en mode sécurisé (option 'Mode' à 1). ). Avec -1 le nombre de réessais est illimité." default="50"/>
</params>
<actions>
<action id="start">
<params>
<param prefix="-inst">instance</param>
<param prefix="-date">date</param>
<param prefix="-pserver">server</param>
<param prefix="-port">port</param>
<param prefix="-starttime">starttime</param>
<param prefix="-endtime">endtime</param>
<param prefix="-lres">lres</param>
<param prefix="-sendmode">sendmode</param>
<param prefix="-retry">retry</param>
</params>
</action>
</actions>
</Job>
</root>
Мне нужно преобразовать это в POJO, например:
public class CustomJob implements Comparable<CustomJob>, Serializable {
@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(isAttribute = true)
private String name;
//XML
private ModelCustomJob model;
//ICON
private String icon;
...//standards getters and setters
Я думал о создании модели, как эта:
@JacksonXmlRootElement(localName = "root")
public class ModelCustomJob {
@JacksonXmlProperty(localName = "Job")
@JacksonXmlElementWrapper(useWrapping = false)
private List<CustomJob> customJobs;
public ModelCustomJob(List<CustomJob> customJobs) {
this.customJobs = customJobs;
}
public List<CustomJob> getCustomJobs() {
return customJobs;
}
public void setCustomJobs(List<CustomJob> customJobs) {
this.customJobs = customJobs;
}
}
Затем используйте отдельный десериализатор:
public class CustomJobIconDeserializer extends ValueGenElemDeserializerToOne<CustomJob> {
@Override
public List<CustomJob> deserialize(GenList dataToSerialize) throws NotFoundVariableDateException, NotFoundModelingServerException {
//XML
XmlMapper mapper = new XmlMapper();
try {
ModelCustomJob modelCustomJob = mapper.readValue(XML, ModelCustomJob.class);
System.out.println(modelCustomJob);
} catch (IOException e) {
e.printStackTrace();
}
//Custom job et icon
return null;
}
Но я получаю следующее исключение:
com.fasterxml.jackson.core.JsonParseException: Unexpected character 'x' (code 120) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.fasterxml.jackson.dataformat.xml.util.StaxUtil.throwAsParseException(StaxUtil.java:37)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._initializeXmlReader(XmlFactory.java:657)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:554)
at com.fasterxml.jackson.dataformat.xml.XmlFactory.createParser(XmlFactory.java:433)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
at com.axway.automator.webui.exchange.tools.valueDeserializer.impl.CustomJobIconDeserializer.deserialize(CustomJobIconDeserializer.java:32)
at com.axway.automator.webui.exchange.transaction.connectLoad.ConnectLoadDeserializer.deserialize(ConnectLoadDeserializer.java:31)
at com.axway.automator.webui.exchange.transaction.connectLoad.ConnectLoadDeserializer.deserialize(ConnectLoadDeserializer.java:17)
at com.axway.automator.webui.exchange.exchange.ExchangeQueryModeling.getConfigurationManager(ExchangeQueryModeling.java:137)
at com.axway.automator.api.service.impl.ConnectionServiceImpl.login(ConnectionServiceImpl.java:196)
at com.axway.automator.api.controller.ConnectionController.login(ConnectionController.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character 'x' (code 120) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1]
at com.ctc.wstx.sr.StreamScanner.throwUnexpectedChar(StreamScanner.java:653)
at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2133)
at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1181)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._initializeXmlReader(XmlFactory.java:653)
... 57 more
Я попытался использовать этот пример и этот, но я все еще немного далек от решения. Любая помощь будет оценена.