我从客户端得到一个 HttpservletRequest,我正在尝试解析请求(使用 JAXB,DOM)。为此,我正在使用以下代码
String str_request = IOUtils.toString(request.getInputStream());
System.out.println("String is " + str_request);
requestStream = new ByteArrayInputStream(str_request.getBytes());
我的问题是,当我将requestStream传递给我的解析器方法时,一种方法工作正常,但另一种方法失败。我猜 inputStream 有问题,但我无法解决这个问题。任何人都可以为这个问题提出解决方案。
DOM解析器方法:
public String parseMethodName(InputStream request) throws SAXException,
IOException,
ParserConfigurationException {
System.out.println("in parseMethodName");
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
System.out.println("bfor parse");
Document doc = dBuilder.parse(request);
System.out.println("After parse");
methodName =
doc.getElementsByTagName("methodName").item(0).getTextContent();
}
JAXB 解析器方法:
System.out.println("in parse ******");
JAXBContext context = JAXBContext.newInstance(MethodCall.class);
System.out.println("bfor unmarshall ****");
Unmarshaller um = context.createUnmarshaller();
System.out.println("After unmarshall ****");
mc = (MethodCall) um.unmarshal(is);
我收到以下异常:
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.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)