java tool to create client-side stubs for REST service

22,419

Solution 1

Assuming you use CXF and you have a generated class for a service endpoint BookStore

BookStore store = JAXRSClientFactory.create("http://bookstore.com",
                                            BookStore.class);
Books books = store.getAllBooks();

See the following links for details:

Solution 2

If you do know maven you can use wadl2java maven plugin here is sample way to use.

<plugin>
                        <groupId>org.apache.cxf</groupId>
                        <artifactId>cxf-wadl2java-plugin</artifactId>
                        <version>2.7.6</version>
                        <executions>
                            <execution>
                                <id>generate-sources</id>
                                <phase>generate-sources</phase>
                                <configuration>
                                    <sourceRoot>${basedir}/target/generated/src/main/java</sourceRoot>
                                    <wadlOptions>
                                        <wadlOption>
                                            <wadl>${basedir}/src/main/resources/wadl/kp.wadl</wadl>
                                            <impl>true</impl>
                                            <packagename>com.kp.webservices.service</packagename>
                                            <extraargs>
                                                <extraarg>-supportMultipleXmlReps</extraarg>
                                            </extraargs>
                                        </wadlOption>
                                    </wadlOptions>
                                </configuration>
                                <goals>
                                    <goal>wadl2java</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

Solution 3

Apache CXF can do it.

Here you can find how to generate artifacts from wadl and how to use them as a client.

http://cxf.apache.org/docs/jaxrs-services-description.html

Share:
22,419
Chris K
Author by

Chris K

Software engineer and system admin for nearly 20 years!

Updated on March 21, 2020

Comments

  • Chris K
    Chris K about 4 years

    Assume I'm given a WADL for a REST webservice, and I've been able to put together a bunch of requests in SoapUI (I'm no stranger to REST or SOAP) - and I've managed to get the wadl2java tool to auto-generate and compile the classes from my WADL.

    Is there any tutorial out there demonstrating how to use these classes to access my REST webservice? I'd ideally like to avoid large frameworks (Spring may be nice, but I'd like to keep my dependencies to a minimum at the moment).

    This url offers a hint to use wadl2java, but again, no one seems to provide any examples of actually using the work product in a viable tutorial? create client side java classes from a RESTful service in CXF

    EDIT: I am using the wadl2java maven plugin, which is awesome. Except for one bug I discovered, it worked flawlessly to generate (and compile) the stub code. I'll check out some of the answers proffered below and add my feedback.

    EDIT 13/Mar:
    Maven cxf-wadl2java-plugin created the file: target\generated-sources\cxf\com\example\services\v2\package-info.java:

    @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.example.com/services/v2",
        elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.example.services.v2;
    

    Looks like that's not the easy solution I was hoping for.
    For reference, the error I'm getting is: [com.sun.istack.SAXException2: unable to marshal type "com.example.services.v2.ModelCriteria" as an element because it is missing an @XmlRootE lement annotation]

    Code I finally used:

        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
        bean.setAddress("https://example.com/services/v2/rest");
        bean.setUsername(...);
        bean.setPassword(...);
        bean.setResourceClass(ModelRestService.class);
    
        bean.getOutInterceptors().add( new org.apache.cxf.interceptor.LoggingOutInterceptor() );
    
        ModelRestService model = bean.create(ModelRestService.class);
    
        ModelCriteria mc = oFact.createModelCriteria();
        mc.setModelNumber("Test");
    
        FindModelResult fmResult = model.findByCriteria(mc);
    

    The remaining @XmlRootElement error came about because I wasn't fully qualifying the REST endpoint /services/v2/rest.

  • Chris K
    Chris K about 10 years
    Using your code example gets me one step closer. I'm now getting: unable to marshal type "ModelCriteria" as an element because it is missing an @XmlRootElement annotation. Short of adding this annotation to all my types, I've seen some guidance about wrapping my element in a JAXBElement<>. I suppose that's passable to my stubs?
  • anttix
    anttix about 10 years
    Root element can be defined in package-info file stackoverflow.com/questions/16584555/…