用于Java的REST客户端? [英] REST clients for Java?

查看:223
本文介绍了用于Java的REST客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JSR 311及其实现,我们有一个强大的标准,可以通过Rest公开Java对象。然而,在客户端,似乎缺少可与Apache Axis for SOAP相媲美的东西 - 隐藏Web服务并将数据透明地封送回Java对象。

With JSR 311 and it's implementations we have a powerful standard for exposing Java objects via Rest. However on the client side there seems to be something missing that is comparable to Apache Axis for SOAP - something that hides the web service and marshals the data transparently back to Java objects.

您如何创建Java RESTful客户端?使用HTTPConnection并手动解析结果?或专门的客户,例如Jersey或Apache CXR?

How do you create Java RESTful clients? Using HTTPConnection and manual parsing of the result? Or specialized clients for e.g. Jersey or Apache CXR?

推荐答案

这是一个老问题(2008)所以现在有比以往更多的选项了:

This is an old question (2008) so there are many more options now than there were then:

  • Apache CXF has three different REST Client options
  • Jersey (mentioned above).
  • Spring also has its own called RestTemplate
  • Commons HTTP Client build your own for older Java projects.

UPDATE 大约2014年:

UPDATE circa 2014:

  • Async-http-client by Sonatype. Ning Async-http-client.

块上的新孩子提供NIO支持(虽然我认为这不会真正改善客户端的性能,例如服务器)。

The new kid on the block which provides NIO support (although truthfully I don't think this really improves performance for clients like it does servers).

  • Apache HTTP Components (4.2) Fluent adapter - Better than old Commons HTTP Client 3 and easier to use for building your own REST client. You'll have to use something like Jackson for JSON parsing support and you can use HTTP components URIBuilder to construct resource URIs similar to Jersey/JAX-RS Rest client. HTTP components also supports NIO but I doubt you will get better performance than BIO given the short request nature of REST.

UPDATE 2016


  • OkHttp - 支持更新的HTTP协议(SPDY和HTTP2)。适用于Android。不幸的是,它没有提供真正的基于反应器循环的异步选项(参见上面的Ning和HTTP组件)。但是,如果您使用较新的HTTP2协议,这不是问题(假设连接数有问题)。

  • 改造 - 将基于类似于某些Jersey和CXF扩展的接口存根自动创建客户端。使用OkHttp。

  • Apache HttpComponents 5应该具有HTTP2支持

  • OkHttp - Supports newer HTTP protocols (SPDY and HTTP2). Works on Android. Unfortunately it does not offer a true reactor-loop based async option (see Ning and HTTP components above). However if you use the newer HTTP2 protocol this is less of a problem (assuming connection count is problem).
  • Retrofit - Will auto create clients based on interface stubs similar to some Jersey and CXF extensions. Uses OkHttp.
  • Apache HttpComponents 5 will supposedly have HTTP2 support

关于选择HTTP的警告/ REST客户端。确保检查您的框架堆栈用于HTTP客户端的内容,它如何进行线程化,并且如果它提供了一个,则理想情况下使用相同的客户端。也就是说,如果您使用Vert.x或Play之类的东西,您可能想尝试使用其支持客户端参与框架提供的任何总线或反应器循环......否则请为可能有趣的线程问题做好准备。

A caveat on picking HTTP/REST clients. Make sure to check what your framework stack is using for an HTTP client, how it does threading, and ideally use the same client if it offers one. That is if your using something like Vert.x or Play you may want to try to use its backing client to participate in whatever bus or reactor loop the framework provides... otherwise be prepared for possibly interesting threading issues.

这篇关于用于Java的REST客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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