java tool to create client-side stubs for REST service
Asked Answered
O

3

5

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.

Ossa answered 11/3, 2014 at 19:59 Comment(0)
P
6

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:

Partida answered 11/3, 2014 at 20:10 Comment(2)
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?Ossa
Root element can be defined in package-info file #16585055Partida
L
4

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>
Loree answered 11/3, 2014 at 20:33 Comment(0)
L
2

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

Lento answered 11/3, 2014 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.