简体   繁体   中英

Generate Serializable classes from a rest WADL with maven

I need to generate classes that implement Serializable from a rest WADL using a maven plugin. I am able to do this with WSDL projects by adding a jaxb-bindings.xml but this is not working for WADL project.

Edit: Now I am getting this error when generating the files

Failed to execute goal org.apache.cxf:cxf-wadl2java-plugin:3.2.0:wadl2java (generate-sources) on project my_project: java.lang.RuntimeException: Error compiling schema from WADL : " http://localhost:8080/rest_web/rest/application.wadl " is not a part of this compilation. Is this a mistake for " http://localhost:8080/rest_web/rest/application.wadl/xsd2.xsd "? -> [Help 1]

  1. I figured out that my tag was in the wrong place on the client so i fixed it.
  2. I added the extraarg for -xjc-extension becuase of link to oracle documents from this CXF issue
  3. Updated my jaxb-bindings.xml

web app pom.xml that generates the WADL:

<plugin>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>maven-wadl-plugin</artifactId>
    <version>1.19.4</version>
    <executions>
        <execution>
            <id>generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <phase>prepare-package</phase>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <wadlFile>${project.build.outputDirectory}/application.wadl</wadlFile>
        <formatWadlFile>true</formatWadlFile>
        <baseUri>http://${server}:8080/${project.name}/rest</baseUri>
        <packagesResourceConfig>
            <param>webservice.rest</param>
        </packagesResourceConfig>
        <wadlGenerators>
            <wadlGeneratorDescription>
                <className>com.sun.jersey.server.wadl.generators.WadlGeneratorApplicationDoc
                </className>
                <properties>
                    <property>
                        <name>applicationDocsFile</name>
                        <value>${basedir}/src/main/doc/application-doc.xml</value>
                    </property>
                </properties>
            </wadlGeneratorDescription>
            <wadlGeneratorDescription>
                <className>com.sun.jersey.server.wadl.generators.WadlGeneratorGrammarsSupport</className>
                <properties>
                    <property>
                        <name>grammarsFile</name>
                        <value>${basedir}/src/main/doc/application-grammars.xml</value>
                    </property>
                </properties>
            </wadlGeneratorDescription>
        </wadlGenerators>
    </configuration>
</plugin>

Client pom.xml file:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-wadl2java-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>generate-sources</id>
            <phase>generate-sources</phase>
            <configuration>
                <sourceRoot>${basedir}/target/generated-sources</sourceRoot>
                <wadlOptions>
                    <wadlOption>
                        <wadl>http://localhost:8080/rest_web/rest/application.wadl</wadl>
                        <extraargs>
                            <extraarg>-verbose</extraarg>
                            <extraarg>-xjc-extension</extraarg>
                        </extraargs>
                        <bindingFiles>
                            <bindingFile>${basedir}/src/main/resources/META-INF/jaxb/jaxb-bindings.xml</bindingFile>
                        </bindingFiles>
                        <packagename>org.apache.cxf.systest.jaxrs.codegen.service</packagename>
                        <schemaPackagenames>
                            <schemaPackagename>http://superbooks=org.apache.cxf.systest.jaxrs.codegen.schema</schemaPackagename>
                        </schemaPackagenames>
                    </wadlOption>
                </wadlOptions>
            </configuration>
            <goals>
                <goal>wadl2java</goal>
            </goals>
        </execution>
    </executions>
</plugin>

jaxb-bindings.xml

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
          version="2.1">
    <globalBindings>
        <serializable uid="1" />
    </globalBindings>
</bindings> 

The reason is that you have multiple XSD schemas (imported xsd files or explicitly defined <xs:schema> tags) in <grammars> block. You need one root <xs:schema> tag inside <grammars> block in your wadl to get XJB bindings to work with wadl2java . I'm looking for workaround without wadl editing now but still not succeeded, and I would be really grateful if somebody share the solution.

Best regards, Aliaksei

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