用于为 REST 服务创建客户端存根的 Java 工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22335358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java tool to create client-side stubs for REST service
提问by Chris K
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.
假设我获得了 REST web 服务的 WADL,并且我已经能够在 SoapUI 中组合一堆请求(我对 REST 或 SOAP 并不陌生) - 我已经设法让 wadl2java 工具从我的 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).
是否有任何教程演示如何使用这些类访问我的 REST web 服务?理想情况下,我希望避免使用大型框架(Spring 可能不错,但目前我希望将依赖项保持在最低限度)。
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
这个 url 提供了使用 wadl2java 的提示,但同样,似乎没有人提供任何在可行教程中实际使用工作产品的示例? 从 CXF 中的 RESTful 服务创建客户端 Java 类
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.
编辑:我正在使用 wadl2java maven 插件,这很棒。除了我发现的一个错误之外,它可以完美地生成(和编译)存根代码。我将查看下面提供的一些答案并添加我的反馈。
EDIT 13/Mar:
Maven cxf-wadl2java-plugin created the file: target\generated-sources\cxf\com\example\services\v2\package-info.java:
编辑 13/Mar:
Maven cxf-wadl2java-plugin 创建了文件: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]
看起来这不是我希望的简单解决方案。
作为参考,我得到的错误是:
[com.sun.istack.SAXException2:无法将类型“com.example.services.v2.ModelCriteria”编组为元素,因为它缺少@XmlRootE 元素注释]
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.
剩下的 @XmlRootElement 错误是因为我没有完全限定 REST 端点 /services/v2/rest。
采纳答案by anttix
Assuming you use CXF and you have a generated class for a service endpoint BookStore
假设您使用 CXF 并且您有一个为服务端点生成的类 BookStore
BookStore store = JAXRSClientFactory.create("http://bookstore.com",
BookStore.class);
Books books = store.getAllBooks();
See the following links for details:
有关详细信息,请参阅以下链接:
回答by edubriguenti
Apache CXFcan do it.
Apache CXF可以做到。
Here you can find how to generate artifacts from wadland how to use them as a client.
在这里您可以找到如何从wadl生成工件以及如何将它们用作客户端。
回答by Karthik Prasad
If you do know maven you can use wadl2java maven plugin here is sample way to use.
如果您确实了解 maven,则可以使用 wadl2java maven 插件,这里是使用示例。
<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>