java - 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?
解决方案
当该字段Marshaller.JAXB_FRAGMENT
设置为 true 时,编组器不会调用startDocument()
(and )(例如)。负责将初始 XML 声明写入输出。endDocument()
JAXBWriter
startDocument
因此,要在输出中包含标题,请不要将属性设置JAXB_FRAGMENT
为true
.
推荐阅读
- r - R中有没有办法按“运行”分组?
- c++ - 从父类的向量访问子类函数而不向下转换
- apache-spark - 输出 Hive 表已分桶,但 Spark 当前不填充与 Hive 兼容的分桶输出
- r - 如何从“mirt”包中向绘图添加垂直线?
- java - 我需要java数组来csharp
- python-3.x - Python ANTLR4 示例 - 解析器似乎无法正确解析
- regex - 我如何在没有库的情况下在 lua 中编写这个正则表达式模式?
- javascript - 按数据属性更改元素
- python - 如何从类变量中引用静态方法
- azure-devops - 无法在 Azure DevOps Build Pipeline 中使用 .NET SDK 代理