java - 未显示 XML header

标签 java jaxb

我正在尝试发送 XML 请求。我试过这段代码:

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

我尝试手动设置 XML header <?xml version="1.0" encoding="UTF-8" standalone="yes"?>但是当我打印结果时它没有显示。你知道为什么吗?

最佳答案

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

因此,要在输出中包含 header ,请不要将属性 JAXB_FRAGMENT 设置为 true

关于java - 未显示 XML header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57757221/

相关文章:

java - com.fasterxml.jackson.databind.exc.InvalidDefinitionException : Cannot construct instance of `javax.xml.bind.JAXBElement`

java源解析,找到具有完整类型的方法签名

java - 使用 JAXB 在不同架构版本之间迁移对象

java - JAXB 命名空间和前缀

java - CXF JSONProvider 编码导致 java.lang.ClassNotFoundException : com. sun.xml.bind.marshaller.NamespacePrefixMapper

java - 是否可以为使用 jaxb 生成的类生成 equals 和 compareTo 方法

java - 使用 OkHttp 时 Webapp 取消部署内存泄漏,是否误报?

java - JBOSS部署报错javax.management.InstanceNotFoundException

c# - 使用工具还是手动将 Java 转换为 C#?

java - JAXB 解码中 XML 中的命名空间存在问题