什么是在Java中实施客户端和服务器REST框架的最佳解决方案?我一直在努力寻找易于使用的解决方案。
更新: Jersey和Restlet似乎都是不错的选择。我们可能会使用Restlet,但会尝试两种方式。
什么是在Java中实施客户端和服务器REST框架的最佳解决方案?我一直在努力寻找易于使用的解决方案。
更新: Jersey和Restlet似乎都是不错的选择。我们可能会使用Restlet,但会尝试两种方式。
Jersey 对于编写 Web 服务来说非常简单易用。您可以使用注解来实现:
@Path("/helloworld")
public class HelloWorldResource {
// The Java method will process HTTP GET requests
@GET
// The Java method will produce content identified by the MIME Media
// type "text/plain"
@Produces("text/plain")
public String helloWorld() {
// Return some cliched textual content
return "Hello World";
}
}
对于一个客户端:
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/helloworld");
String s = webResource.get(String.class);
System.out.println(s); // prints Hello World
Restlet听起来应该提供您正在寻找的内容:
Restlet在其2.0版本中支持客户端和服务器端的注释。JAX-RS API也作为扩展得到支持。
以下是一个针对服务器端的简单示例:
public class HelloWorldResource extends ServerResource {
@Get
public String represent() {
return "hello, world";
}
}
在客户端:
// Outputting the content of a Web page
new ClientResource("http://www.restlet.org").get().write(System.out);
如需更多文档,请查看此页面。
个人没有亲自使用过,但我与一些工作团队合作正在使用Spring 3 MVC。Spring 3中的REST:@MVC看起来是一个很好的博客概述。RESTful功能包括“URI模板”,“内容协商”,“HTTP方法转换”,“ETag支持”等。
编辑: 此外,请查看这个问题:有没有人推荐一个基于MVC并支持REST的Java Web框架?
http://incubator.apache.org/wink/
它实现了JAX-RS规范,同时拥有REST开发的客户端和服务器框架。Apache支持这个项目 - 这总是一个好兆头(也是一个好的许可证 :-))。
我最喜欢这个框架的地方就是与Spring的直观集成,如果你想要让你的框架容易配置和扩展,这非常有用。
更新:Xydra Restless不再维护 +++ 如果您在Google AppEngine发布“保留实例”功能之前使用它,您可能需要考虑使用Xydra Restless,它具有一些功能但加载速度快。