我正在尝试从gwt服务器发送编组jaxb数据,并在jboss运行的servlet接收中读取它。
发送部分的代码:
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();
在eclipse调试中,海报输出流如下所示
<?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");
}
}
}
和我得到异常在servlet(接收部分)
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
我发送的信息到达servlet,因为当我打印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
我发现了问题:
-
我需要添加内容类型定义:
。setRequestProperty("内容类型","text/html");
-
我需要@XmlRootElement在xsd生成的代码。我仍然不知道如何生成它。但似乎有很多关于这个
-
我有一个类型转换错误,所以为了学习非编组类型,我打印了非编组对象类
Object 0 = tUnmarshaller.unmarshal(rd);打印o.getClass ();