我正在使用 Spring Boot。 我已经使用 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();
但我收到以下错误消息:
"unable to marshal type \"com.uk.services.consumer.schema.v4.search.searchresponse.QuotationSearchResponse\" as an element because it is missing an @XmlRootElement annotation"}
如何将 apache CXF 创建的对象序列化回 XML。 我正在使用Spring Boot。
<小时/>作为引用,CXF 生成的源代码如下所示。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "QuotationSearchResponse")
public class QuotationSearchResponse
extends CommonResponse
{
}
最佳答案
如果您想要编码(marshal)没有 @XmlRootElement 注释的对象,您可以尝试更改此行:
marshaller.marshal(instance, sw);
进入此(相应地更改 InstanceClass)
marshaller.marshal(new JAXBElement<InstanceClass>(new QName("uri","local"), InstanceClass.class, instance), sw);
关于java - 在Spring Boot中将soap响应转换回xml字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58287674/