Проблемы вывода JAXB: Странное исключение

javax.xml.bind.MarshalException - со связанным исключением: [javax.xml.stream.XMLStreamException: Невозможно вывести объявление XML, после того, как другой вывод уже был сделан.] в com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:330) в com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:178)

Вот мой фрагмент кода:

OutputStream output = new ByteArrayOutputStream();
            XMLOutputFactory xof = XMLOutputFactory.newInstance();
            XMLStreamWriter xsw = xof.createXMLStreamWriter(output);


            QName root = new QName("return");
            JAXBElement<Customer> je = new JAXBElement<Customer>(root, Customer.class, customer);

            xsw.writeStartDocument();
            xsw.setDefaultNamespace("S");
            xsw.writeStartElement("S", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
        xsw.writeStartElement("S", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
        xsw.writeStartElement("ns0", "findCustomerResponse", "http://service.jaxws.blog/");

            context = JAXBContext.newInstance(Customer.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(je, xsw);
            xsw.writeEndDocument();
            xsw.close();
            if(output != null)
            return output.toString();

У вас есть идеи, почему это происходит? заранее спасибо

1 ответ

Вам необходимо указать JAXB_FRAGMENT собственность на Marshaller чтобы избежать этой проблемы. Это свойство позволяет JAXB знать, что он размещается в середине документа и что ему не следует писать заголовок.

Для дополнительной информации

Другие вопросы по тегам