为什么 JAX-WS 是 JDK 的一部分,而 JAX-RS 不是?

Why is JAX-WS part of the JDK but JAX-RS is not?

使用 Eclipse 时,我可以在不包含任何外部依赖项的情况下使用 JAX-WS 注释(例如 @WebService),但我不能对 JAX-RS 注释(例如 @Path).我查看了 this 答案,我了解到 javax.ws.rs 不是 JDK 的一部分。为什么 JAX-WS 是 JDK 的一部分而 JAX-RS 不是? 其次,如果我在 GlassFish 或 WildFly 服务器上部署带 JAX-WS 注释的应用程序,服务器是否使用众所周知的 WebService 堆栈来 运行 应用程序(例如 Metro)或其自己的实现?

JAX-RS(RESTful Web 服务的 Java API)实际上是一个定义 REST 支持的规范。 该规范是通过 Java 规范请求 (JSR) 311 定义的。

简而言之,本规范定义了一个 API 和所有兼容的 JAX-RS 实现都应使用的一些注释。因此,该规范本身由创建 JAX-RS 实现(例如 Jersey)的 JAX-RS 实现者和可以以相同方式使用任何兼容的 JAX-RS 实现的程序员(最终用户)使用。

为了使用 JAX-RS,您需要 JAX-RS 的实现。因此,如果一个不包含在 JDK 中,则可以使用一个外部的。 JDK 中应包含或不包含哪些实现的实际决定很复杂,一些有趣的标准可能是实现的成熟度和接受度,以及使用频率。 (很少使用的功能可以额外下载。)

此答案使用此处提供的信息:

http://www.vogella.com/tutorials/REST/article.html#restjersey

对于问题的第二部分:服务器使用捆绑实现。例如 Glassfish 将 Metro 用于 JAX-WS,但这取决于服务器。

JAX-WS:Java API 用于 XML Web 服务

让我们回到 2006 年。Java SE 6 发布,引入了许多新功能。

引用Java SE 6规范(JSR 270),关于Java SE 6中引入的特性:

The feature set for Java SE 6 is driven, in large part, by a set of themes.

The themes describe the main focal points of the release. Some themes are fairly abstract guiding principles; others are more concrete in that they identify particular problem areas, significant new feature sets, or specific target market segments.

[...]

  • XML & Web Services: The Java SE 5 release, as originally proposed, was intended to include a full Web Services client stack. That work unfortunately could not be completed in time for that release, and in the meantime XML and Web Services have only increased in their importance to many members of the community.

[...]

JAX-WS 2.0 规范的目标之一是准备 JAX-WS 以包含在 J2SE 的未来版本中(已重命名为 Java SE 之后)。引用 JSR 224:

  • Inclusion in J2SE: JAX-WS 2.0 will prepare JAX-WS for inclusion in a future version of J2SE. Application portability is a key requirement and JAX-WS 2.0 will define mechanisms to produce fully portable clients.

Java SE 6 包含 JAX-WS 2.0 组件,引入了在 Java SE 中创建基于 SOAP 的 Web 服务的可能性。引用来自 Oracle 的 this article

One of the most exciting new features of the Java Platform, Standard Edition 6 (Java SE 6) is support for the Java API for XML Web Services (JAX-WS), version 2.0. JAX-WS 2.0 is the center of a newly rearchitected API stack for web services [...].

Although JAX-WS finds its main home in the open-source world of the Java Platform, Enterprise Edition 5 (Java EE 5) and is designed to take the place of Java API for XML-Based RPC (JAX-RPC) in such an environment, you can reuse much of the functionality without even touching an enterprise server [...]

You can use JAX-WS to build web applications and web services, incorporating the newer XML-based web services functionality. [...]

When you run the application, the Java SE 6 platform has a small web application server that will publish the web service. [...]

JAX-RS:Java API 用于 RESTful Web 服务

JAX-RS 于 2008 年出现。它最初由 JSR 311 and it was included under the Java EE 6 umbrella specification (JSR 316) 定义。

JAX-RS 的第二个版本于 2013 年问世,由 JSR 339 and it was included under the Java EE 7 umbrella specification (JSR 342) 定义。

JAX-RS 以 HTTP 为中心,JAX-RS 应用程序经常部署在 servlet 容器上。

Java SE 7 (JSR 336) and the Java SE 8 (JSR 337) specifications don't incorporate the JAX-RS component. However, JAX-RS applications can be published in Java SE environments (using RuntimeDelegate) 和 JAX-RS 实现也可能支持通过 JAX-WS 发布。

在 Java SE 中包含 JAX-RS 也意味着包含该规范的实现。它使 Java SE 环境中的事情变得更加复杂,但在容器将为您提供实现的 Java EE 环境中完全可以接受。例如,GlassFish 为您提供 Jersey(参考实现),而 JBoss/WildFly 为您提供 RESTEasy。