JAX-RS非常适合实现REST.你用什么来用Java调用REST服务?

ave*_*net 7 java rest client jax-rs

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

jos*_*chi 9

JAX-RS(JSR311)没有定义客户端API,但大多数JAX-RS实现都有一个,如Jersey,RESTeasyApache CXF.Restlet框架还具有客户端支持以及单独的HTTP客户端扩展.

由于这些是REST的专用库/框架,我建议你研究这些.


Avi*_*lax 7

你在评论中写道,你"希望得到比HttpClient更高级别的东西".听起来Restlet会很完美.它提供了一个用于实现和使用 RESTful Web应用程序的高级API,以及用于较低级别实现的即插即用适配器.

例如,要使用Restlet 1.1将webform发布到资源:

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());
}
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

HTH!