0

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
{   
}
Jim
  • 11,197
  • 11
  • 66
  • 151
  • Have a look at https://stackoverflow.com/questions/819720/no-xmlrootelement-generated-by-jaxb/49083876#49083876 – Michal Oct 08 '19 at 14:04

1 Answers1

1

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);
Villat
  • 1,354
  • 11
  • 31
  • Hi @Villat, that's great thanks, would you mind sharing how to rehydrate the java class from the uri/local XML, are there any caveats with reloading the XML? – Jim Oct 09 '19 at 06:06