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

查看:34
本文介绍了如何为 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:

更新(2020 年仍处于活动状态的项目):

  • Apache HTTP 组件 (4.2) Fluent 适配器 - JDK 的基本替代品,被此列表中的其他几个候选者使用.比旧的 Commons HTTP Client 3 更好,更容易用于构建自己的 REST 客户端.您必须使用类似 Jackson 的 JSON 解析 支持,您可以使用 HTTP组件URIBuilder构建资源URI类似于Jersey/JAX-RS Rest客户端.HTTP 组件也支持 NIO,但我怀疑考虑到 REST 的短请求特性,你会获得比 BIO 更好的性能.Apache HttpComponents 5 支持 HTTP/2.
  • OkHttp - JDK 的基本替代品,类似于 http 组件,使用由此列表中的其他几位候选人.支持较新的 HTTP 协议(​​SPDY 和 HTTP2).适用于Android.不幸的是,它没有提供真正的基于反应器循环的异步选项(参见上面的 Ning 和 HTTP 组件).但是,如果您使用较新的 HTTP2 协议,则问题不大(假设连接数有问题).
  • Ning Async-http-client - 提供 NIO支持.以前称为 Async-http-client奏鸣曲.
  • Feign 低级 http 客户端的包装器(okhttp、apache httpcomponents).基于类似于某些 Jersey 和 CXF 扩展的接口存根自动创建客户端.强大的弹簧集成.
  • Retrofit - 低级 http 客户端 (okhttp) 的包装器.基于类似于某些 Jersey 和 CXF 扩展的接口存根自动创建客户端.
  • Volley 用于 jdk http 客户端的包装器,由 google 提供
  • google-http 用于 jdk http 的包装器客户端,或 apache httpcomponents,由 google 提供
  • Unirest 用于 jdk http 客户端的包装器,来自 kong
  • Resteasy 用于 jdk http 客户端的 JakartaEE 包装器,由 jboss,jboss 框架的一部分
  • jcabi-http apache httpcomponents 的包装器,jcabi 的一部分收藏
  • restlet Apache httpcomponents 的包装器,restlet 的一部分框架
  • rest-assured 带有易于测试的断言的包装器
  • 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天全站免登陆