Исключение jaxb для маршаллинга / демаршаллинга в сервлете jboss
Я пытаюсь отправить маршализованные данные jaxb с сервера gwt и прочитать их при получении в сервлете, запущенном jboss.
Это код в отправляющей части:
url = new URL(MessageFormat.format(urlStr,ip));
conn = url.openConnection();
conn.setReadTimeout(ApplicationSettings.instance.m_nUrlReadTimeout_ms);
conn.setConnectTimeout(ApplicationSettings.instance.m_nUrlConnectTimeout_ms);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter outStream = new OutputStreamWriter( conn.getOutputStream());
Marshaller tmarshaller;
JAXBContext tJAXB= JAXBContext.newInstance(path.to.application.jaxb.componentCurrentKPIs.ComponentCurrentKPIs.class);
tmarshaller = tJAXB.createMarshaller();
tmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
tmarshaller.marshal(new JAXBElement<ComponentCurrentKPIs>(new QName("componentCurrentKPIs"), ComponentCurrentKPIs.class, currentTableChanges), outStream);
outStream.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = reader.readLine();
reader.close();
outStream.close();
В отладке затмения поток вывода постера выглядит следующим образом
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<componentCurrentKPIs command="http://{0}:8080/monserver/SetCurrentKpis/?user=xxxx&password=xxxx">
<CurrentKPI value="interface0:interface1" name="Interface"/>
<CurrentKPI value="false:false" name="Enable"/>
<CurrentKPI value="interface:interface1" name="PassThroughPort"/>
</componentCurrentKPIs>
Это приемная часть
public void processRequest(HttpServletRequest pRequest, HttpServletResponse pResponse)throws IOException
{
//To avoid new request while a request is processed
synchronized (lock)
{
try
{
JAXBContext tJAXB = JAXBContext.newInstance("path.to.class.supervisor.jaxb.componentCurrentKPIs");
tJAXB = JAXBContext.newInstance(ComponentCurrentKPIs.class);
Unmarshaller tUnmarshaller = tJAXB.createUnmarshaller();
JAXBElement model = null;
BufferedReader rd = new BufferedReader(new InputStreamReader(pRequest.getInputStream()));
model = (JAXBElement)tUnmarshaller.unmarshal(rd);
ComponentCurrentKPIs currentKpis = (ComponentCurrentKPIs) model.getValue();
for(CurrentKPI kpi : currentKpis.getCurrentKPI())
{
Logger.getLogger(SetCurrentKpis.class).debug("in Set Current Kpis !" + kpi.getName() + " = " + kpi.getValue());
}
} catch (Exception e)
{
Logger.getLogger(SetCurrentKpis.class).error("Exception !" ,e);
}
PrintWriter out = pResponse.getWriter();
out.println("aaaaaaaaaaaaaaaaa");
}
}
}
и я получаю исключение в сервлете (принимающая часть)
18-06-2013 18:40:22 ERROR 131 path.to.servlets.SetCurrentKpis - Exception !
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:172)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
at path.to.applicationName.servlets.SetCurrentKpis.processRequest(SetCurrentKpis.java:120)
at path.to.applicationName.servlets.infrastructure.AbstractOperationServlet.doPost(AbstractOperationServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at path.to.applicationName.servlets.LoginFilter.doFilter(LoginFilter.java:86)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
... 29 more
Информация, которую я отправляю, поступает в сервлет, потому что когда я печатаю заголовки и параметры http-запроса, я вижу его.
connection : keep-alive
content-type : application/x-www-form-urlencoded
content-length : 353
user : xxxx
amp;password : xxxx">
<CurrentKPI value="interface0:interface1" name="Interface"/>
<CurrentKPI value="false:false" name="Enable"/>
<CurrentKPI value="interface0:interface1" name="PassThroughPort"/>
</componentCurrentKPIs>
password : xxxx
<?xml version : "1.0" encoding="UTF-8" standalone="yes"?>
<componentCurrentKPIs command="http://{0}:8080/monserver/SetCurrentKpis/?user=xxxx
blablanull
1 ответ
Я нашел проблемы:
Мне нужно было добавить определение типа контента:
conn.setRequestProperty ("Content-Type", "text / html");
Мне нужно было @XmlRootElement в xsd сгенерированном коде. Я до сих пор не нашел, как его генерировать. Но, похоже, об этом много
У меня была ошибка приведения, поэтому, чтобы выучить тип unmarshalled, я распечатал класс объекта unmarshalled
Объект o = tUnmarshaller.unmarshal(rd); print o.getClass();