Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I would like to add @XmlRoot annotation on CXF codegen

I have a WSDL file which contains the following entry:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="urn:CP_Ablakido" xmlns:s0="urn:CP_Ablakido" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <wsdl:types>
    <xsd:schema elementFormDefault="qualified" targetNamespace="urn:CP_Ablakido">
      <xsd:element name="GetList_11" type="s0:InputMapping1"/>
      <xsd:complexType name="InputMapping1">
        <xsd:sequence>
          <xsd:element name="Qualification" type="xsd:string"/>
          <xsd:element name="startRecord" type="xsd:string"/>
          <xsd:element name="maxLimit" type="xsd:string"/>
        </xsd:sequence>
      </xsd:complexType>
      <xsd:element name="GetList_11Response" type="s0:OutputMapping1"/>
      <xsd:complexType name="OutputMapping1">
        <xsd:sequence>
          <xsd:element maxOccurs="unbounded" name="getListValues">
            <xsd:complexType>....

I use the CXF Codegen plugin with the following settings:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>${basedir}/target/generated-sources/wsdl2java</sourceRoot>
                <encoding>UTF-8</encoding>
            </configuration>
            <goals>
                <goal>wsdl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

And the generated java code look like:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OutputMapping1", propOrder = {
    "getListValues"
})
public class OutputMapping1 {

    @XmlElement(required = true)
    protected List<OutputMapping1 .GetListValues> getListValues;

The problem is that the @XmlRootElement is missing from here. There was another similar questions like

  1. maven-cxf-codegen-plugin using Jaxb binding to add inheritance for all generated classes
  2. Annotating CXF (wsdl2java) generated package
  3. externally create jaxb annotations for class

As the other answers mentioned that I can put bindings file. So I created a binding file with the following content:

<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:annox="http://annox.dev.java.net"
version="2.0">
        <jaxb:bindings node="//xsd:element[@name='GetList_11Response']">
            <annox:annotate target="class">
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="GetList_11Response"/>
          </annox:annotate>
        </jaxb:bindings>
</jaxb:bindings>

And I've added the following block to POM.XML:

<wsdlOptions>
    <wsdlOption>
        <wsdl>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.wsdl</wsdl>
        <bindingFiles>                                       
            <bindingFile>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.xjb
            </bindingFile>
        </bindingFiles>
    </wsdlOption>
</wsdlOptions>

After that I've got error message:

com.sun.istack.SAXParseException2; systemId: file:/Project/icp-integration/icpiCameI/src/main/resources/wsdl/CP_Ablakido_1.xjb; lineNumber: 9; columnNumber: 72; XPath evaluation of "//xs:element[@name='GetList_11Response']" results in empty target node
    at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError(Internalizer.java:624)
    at com.sun.tools.xjc.reader.internalizer.Internalizer.reportError(Internalizer.java:618)
    at com.sun.tools.xjc.reader.internalizer.Internalizer.buildTargetNodeMap(Internalizer.java:294)
    at com.sun.tools.xjc.reader.internalizer.Internalizer.buildTargetNodeMap(Internalizer.java:390)
    at com.sun.tools.xjc.reader.internalizer.Internalizer.transform(Internalizer.java:146)

So I don't know how exactly can I describe that when the complexType is OutputMapping1 then put @XmlRootElement in OutputMapping1.java with the name of "GetList_11Response".

like image 403
Csákány Róbert Avatar asked Dec 12 '15 11:12

Csákány Róbert


People also ask

How do I generate a class from WSDL using Apache CXF?

You will have to make sure that you create an appropriate directory structure for your project and add the earlier shown hello. wsdl file to the specified folder. The wsdl2java plugin will compile this wsdl and create Apache CXF classes in a pre-defined folder.


1 Answers

I found the solution. It was trickey, because:

  1. Have to handle that the XSD is inlined in WSDL. (The trick is schemaLocation="CP_Ablakido_1.wsdl#types1". It tells the JAXB that use node in WSDL file.)
  2. Have to configure CXF to use XJC plugin (annox).

Have to add the following fragments to POM.XML:

<wsdlOption>
    <wsdl>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.wsdl</wsdl>
    <bindingFiles>
        <bindingFile>${basedir}/src/main/resources/wsdl/CP_Ablakido_1.xjb</bindingFile> 
    </bindingFiles>
    <extraargs><extraarg>-xjc-Xannotate</extraarg></extraargs>
</wsdlOption>

and dependencies have to add to plugin:

<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics-annotate</artifactId>
    <version>0.6.0</version>
</dependency>   

<dependency>
    <groupId>org.apache.cxf.xjcplugins</groupId>
    <artifactId>cxf-xjc-ts</artifactId>
    <version>3.0.5</version>
</dependency>

The XJB file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    jaxb:extensionBindingPrefixes="annox xjc"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:annox="http://annox.dev.java.net"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance">

  <jaxb:bindings schemaLocation="CP_Ablakido_1.wsdl#types1" node="/xs:schema"> 
       <jaxb:bindings node="//xs:complexType[@name='InputMapping1']">
            <annox:annotate target="class">
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="GetList_11"/>
          </annox:annotate>
      </jaxb:bindings>    
      <jaxb:bindings node="//xs:complexType[@name='OutputMapping1']">
            <annox:annotate target="class">
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="GetList_11Response"/>
          </annox:annotate>
      </jaxb:bindings>
   </jaxb:bindings>
</jaxb:bindings>
like image 177
Csákány Róbert Avatar answered Nov 10 '22 10:11

Csákány Róbert