首页 > 解决方案 > XML header is not shown

问题描述

I'm trying to send XML request. I tried this code:

        PaymentTransaction pt = new PaymentTransaction();
        ........

        JAXBContext jaxbContext = JAXBContext.newInstance(PaymentTransaction.class);

        // Create Marshaller
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        // Print XML String to Console
        StringWriter sws = new StringWriter();
        jaxbMarshaller.marshal(pt, sws);

        // Verify XML Content
        String xmlContent = sws.toString();

        Mono<PaymentResponse> result = client.executeAndReceive(xmlContent);

I tried to set manually the XML header <?xml version="1.0" encoding="UTF-8" standalone="yes"?> but it's not displayed when I print the result. Do you know why?

标签: javajaxb

解决方案


当该字段Marshaller.JAXB_FRAGMENT设置为 true 时,编组器不会调用startDocument()(and )(例如)。负责将初始 XML 声明写入输出。endDocument()JAXBWriterstartDocument

因此,要在输出中包含标题,请不要将属性设置JAXB_FRAGMENTtrue.


推荐阅读