java - 在 Spring Boot 中将肥皂响应转换回 xml 字符串
问题描述
我正在使用弹簧靴。我已经使用 apache CXF 生成了肥皂请求/响应对象。
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.3.3</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated-sources</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/wsdl/...</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
我想将 Web 服务执行后收到的 JAXB 请求对象序列化回 xml以将其存储在磁盘上以供以后重用。
我试过这个:
JAXBContext context = JAXBContext.newInstance(instance.getClass().getPackage().getName());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
marshaller.marshal(instance, sw);
String xml = sw.toString();
但我收到以下错误消息:
“无法将类型 \"com.uk.services.consumer.schema.v4.search.searchresponse.QuotationSearchResponse\" 编组为元素,因为它缺少 @XmlRootElement 注释”}
如何将 apache CXF创建的对象序列化回XML。我正在使用Spring Boot。
供参考,从 CXF 生成的源看起来像这样。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "QuotationSearchResponse")
public class QuotationSearchResponse
extends CommonResponse
{
}
解决方案
如果要编组没有 @XmlRootElement 注释的对象,可以尝试更改此行:
marshaller.marshal(instance, sw);
进入这个(相应地改变InstanceClass)
marshaller.marshal(new JAXBElement<InstanceClass>(new QName("uri","local"), InstanceClass.class, instance), sw);
推荐阅读
- flutter - Flutter MQTT Client Detached socket 不能同时实现'Stream
- >'和'流
' - winapi - 如何将 DirectX 纹理复制到表面?
- php - 无论如何要从 GCP 连接到外部数据库?(来自 PHP - Laravel 应用程序)
- javascript - 传单插件未加载
- time-complexity - 总和等于给定数 n 的最小平方数的时间复杂度
- docker - Traefik 未检测到在 network_mode=host (192.168.99.x) 中运行的容器
- html - 全部大写 在 Firefox 的下拉列表中选择选项值
- emacs - 为什么键绑定会导致 Emacs 在启动时执行我的功能?
- date - 表格排序日期不正确
- node.js - 用户与服务器断开连接时无法获取 voiceStateUpdate