如何为Java创建REST客户端? [英] How do you create a REST client for Java?

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

问题描述

有了JSR 311及其实现,我们有了一个强大的标准,可以通过REST公开Java对象.但是,在客户端,似乎缺少与SOAP的Apache Axis类似的东西-隐藏了Web服务并将数据透明地封送回Java对象的东西.

With JSR 311 and its 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 RestTemplate superceded by Spring WebClient
  • Commons HTTP Client build your own for older Java projects.

更新(2020年仍在开展中的项目):

  • Apache HTTP组件(4.2)Fluent适配器 -JDK的基本替代品,此列表中的其他一些候选者也使用过.比旧的Commons HTTP Client 3更好,并且更容易用于构建自己的REST客户端.您必须使用 Jackson之类的内容进行JSON解析支持,并且可以使用 OkHttp -使用了类似于http组件的JDK基本替换名单中的其他几位候选人.支持更新的HTTP协议(SPDY和HTTP2).适用于Android.不幸的是,它没有提供真正的基于反应堆循环的异步选项(请参见上面的Ning和HTTP组件).但是,如果您使用较新的HTTP2协议,则问题不大(假设连接计数有问题).
  • 宁Async-http-client -提供NIO支持.以前称为 Async-http-client Sonatype .
  • 伪装 包装器,用于较低级别的HTTP客户端(okhttp,apache httpcomponents) .基于类似于某些Jersey和CXF扩展名的接口存根自动创建客户端.强大的弹簧集成.
  • Retrofit -适用于较低级别http客户端(okhttp)的包装.根据类似于某些Jersey和CXF扩展名的接口存根自动创建客户端.
  • Google的
  • Volley 包装器,用于jdk http客户端
  • google-http 包装器,用于jdk http客户端或apache httpcomponents,由Google
  • Unirest 包装器,由kong li>
  • Resteasy 用于jdk http客户端的JakartaEE包装器,由jboss(jboss框架的一部分)提供
  • jcabi-http 的包装,用于Apache httpcomponents(jcabi的一部分)集合
  • restlet 的包装,用于Apache httpcomponents,是restlet的一部分框架
  • 其他保证 带有断言的包装器,可轻松进行测试
  • Apache HTTP Components (4.2) Fluent adapter - Basic replacement for JDK, used by several other candidates in this list. 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 requestnature of REST. Apache HttpComponents 5 has HTTP/2 support.
  • OkHttp - Basic replacement for JDK, similar to http components, used by several other candidates in this list. 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).
  • Ning Async-http-client - provides NIO support. Previously known as Async-http-client by Sonatype.
  • Feign wrapper for lower level http clients (okhttp, apache httpcomponents). Auto-creates clients based on interface stubs similar to some Jersey and CXF extensions. Strong spring integration.
  • Retrofit - wrapper for lower level http clients (okhttp). Auto-creates clients based on interface stubs similar to some Jersey and CXF extensions.
  • Volley wrapper for jdk http client, by google
  • google-http wrapper for jdk http client, or apache httpcomponents, by google
  • Unirest wrapper for jdk http client, by kong
  • Resteasy JakartaEE wrapper for jdk http client, by jboss, part of jboss framework
  • jcabi-http wrapper for apache httpcomponents, part of jcabi collection
  • restlet wrapper for apache httpcomponents, part of restlet framework
  • rest-assured wrapper with asserts for easy testing

关于选择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天全站免登陆