سؤال

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.

هل كانت مفيدة؟

المحلول

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:

نصائح أخرى

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>

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top