JAX-RS非常适合实现REST.您如何用Java调用REST服务? [英] JAX-RS is perfect for implementing REST. What do you use to call REST services in Java?

查看:116
本文介绍了JAX-RS非常适合实现REST.您如何用Java调用REST服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我正在寻找类似于JAX-RS的东西(使用批注来描述我要调用的服务),但允许调用使用其他技术(不是JAX-RS)实现的REST服务.有什么建议吗?

Ideally, I am looking for something like JAX-RS (using annotations to describe the services I want to call), but allowing to call REST services implemented using other technologies (not JAX-RS). Any suggestion?

推荐答案

您在注释中写道,您希望获得比HttpClient更高级的功能".听起来 Restlet 会是完美的.它提供了用于实现和使用 RESTful Web应用程序的高级API,以及用于较低级别实现的即插即用适配器.

You wrote in a comment that you were "hoping for something more high level" than HttpClient. It sounds like Restlet would be perfect. It provides a high-level API for implementing and using RESTful web applications, with plug-and-play adapters for the lower-level implementations.

例如,使用Restlet 1.1将网络表单发布到资源中:

For example, to POST a webform to a resource using Restlet 1.1:

Client client = new Client(Protocol.HTTP);

Form form = new Form();
form.add("foo", "bar");
form.add("abc", "123");

Response response = client.post("http://host/path/to/resource", form.getWebRepresentation())

if (response.getStatus().isError()) {
    // deal with the error
    return;
}

if (response.isEntityAvailable()) {
    System.out.println(response.getEntity().getText());
}

如果需要在请求上设置更多选项,则可以使用Request对象:

If you need to set more options on the request, you can use a Request object:

Form form = new Form();
form.add("foo", "bar");
form.add("abc", "123");

Request request = new Request(Method.POST, "http://host/path/to/resource");

request.setEntity(form.getWebRepresentation());

request.setReferrerRef("http://host/path/to/referrer");

Response response = client.handle(request);

HTH!

这篇关于JAX-RS非常适合实现REST.您如何用Java调用REST服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆