java 服务端 rest,Java工具为REST服务创建客户端存根

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值