Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java tool to create client-side stubs for REST service

Tags:

java

rest

cxf

wadl

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.

like image 524
Chris K Avatar asked Mar 11 '14 19:03

Chris K


2 Answers

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:

  • http://cxf.apache.org/docs/jax-rs-client-api.html
  • http://cxf.547215.n5.nabble.com/How-to-generate-rest-client-with-wadl2java-td5738281.html
like image 183
anttix Avatar answered Nov 15 '22 03:11

anttix


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>
like image 38
Karthik Prasad Avatar answered Nov 15 '22 03:11

Karthik Prasad