我正在尝试发送 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/