简体   繁体   中英

converting a soap response back to an xml string in Spring Boot

I am using spring boot. I have generated soap request / response objects using 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>

I want to serialize the JAXB request object that I receive once the web service is executed back to xml to store it on disk for later reuse.

I have tried this:

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

But I get the error message below:

"unable to marshal type \"com.uk.services.consumer.schema.v4.search.searchresponse.QuotationSearchResponse\" as an element because it is missing an @XmlRootElement annotation"}

How do I serialize the objects created by apache CXF back to XML . I am using Spring Boot .


for reference the generated source from CXF looks like this.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "QuotationSearchResponse")
public class QuotationSearchResponse
    extends CommonResponse
{   
}

If you want to marshal an object that doesn't have the @XmlRootElement annotation, you could try to change this line:

marshaller.marshal(instance, sw);

Into this (change InstanceClass accordingly)

marshaller.marshal(new JAXBElement<InstanceClass>(new QName("uri","local"), InstanceClass.class, instance), sw);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM