繁体   English   中英

套接字异常:使用JAXB解组时重置连接

[英]Socket exception: Connection reset when unmarshalling with JAXB

我能够使用FileInputStream从xml文件解编为一个类以读取xml内容,并且在解编组代码中使用InputStream而不是FileInputStream遇到了问题。

元帅:

try {
    JAXBContext jaxbContext = JAXBContext.newInstance(Message.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        JAXBElement<Message> je = 
            new JAXBElement<Message> (new QName(Message.class.getSimpleName()), Message.class, message);

    jaxbMarshaller.marshal(je, os);
} catch (JAXBException e) {
    e.printStackTrace();
}

解组

JAXBContext jc = null;
try {       
    jc = JAXBContext.newInstance(Message.class);            
    Unmarshaller um = jc.createUnmarshaller();
    JAXBElement<Message> je = (JAXBElement<Message>) um.unmarshal(new StreamSource(is), Message.class);
    message = je.getValue();
} catch (JAXBException | FileNotFoundException e) {
    e.printStackTrace();
}

我得到的错误:

javax.xml.bind.UnmarshalException
 - with linked exception:
[java.net.SocketException: Connection reset]

尝试这样的事情。

使用JAXBIntrospector来获取值。

String filepath="C:\\somepath";
FileInputStream xml = new FileInputStream(filepath);
Object result = unmarshaller.unmarshaller.unmarshal(xml);
Message msg = (Message) JAXBIntrospector.getValue(result);

StreamSource xml = new StreamSource(filepath);
JAXBElement<Message> msgclass = 
unmarshaller.unmarshal(xml, Message.class);

“连接重置”与XML,JAXB,解组等无关。通常是由于写入已被对等方关闭的连接而引起的。 换句话说,应用程序协议错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM