用于Java的REST客户端?

编程入门 行业动态 更新时间:2024-10-07 23:21:38
本文介绍了用于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 有三种不同的 REST客户端选项
  • 泽西岛 (如上所述)。
  • Spring 也有自己的名字 RestTemplate
  • Commons HTTP Client 为较旧的Java构建自己的项目。
  • 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 。
  • 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组件(4.2)Fluent适配器 - 比旧的Commons HTTP Client 3更好,更易于用于构建自己的REST客户端。你必须使用 Jackson for JSON parsing 支持,你可以使用 HTTP组件URIBuilder构建资源URI 类似于Jersey / JAX-RS Rest客户端。 HTTP组件也支持NIO但我怀疑你会得到比BIO短的请求性质更好的性能。
  • 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客户端?

本文发布于:2023-11-13 02:43:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   Java   REST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!