简体   繁体   中英

Socket exception: Connection reset when unmarshalling with JAXB

I was able to unmarshal from xml file to a class using FileInputStream to read the xml content, and I'm having a problem using InputStream instead of FileInputStream in the unmarshaling code.

marshal:

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();
}

Unmarshal:

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();
}

The error I'm getting:

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

Try something like this.

Use JAXBIntrospector to get the value.

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);

'Connection reset' has nothing to do with XML, JAXB, unmarshalling, etc. It is usually caused by writing to a connection that had already been closed by the peer. In other words, an application protocol error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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