常见Jersey示例

2.3.1 基于 Servlet 的 GlassFish 应用

如果你使用 GlassFish 应用服务,那么你不需要打包任何东西,所有的一切都已经包含在其中了。你只需要在你的应用中声明依赖使用 JAX-RS API 即可。

  1. <dependency>
  2. <groupId>javax.ws.rs</groupId>
  3. <artifactId>javax.ws.rs-api</artifactId>
  4. <version>2.0.1</version>
  5. <scope>provided</scope>
  6. </dependency>

如果你使用特定的功能,那么直接使用特定的 Jersey 依赖即可:

  1. <dependency>
  2. <groupId>org.glassfish.jersey.containers</groupId>
  3. <artifactId>jersey-container-servlet</artifactId>
  4. <version>2.23.2</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <!-- 如果你只使用 Jersey 客户端具体功能而不包含服务端的 -->
  8. <dependency>
  9. <groupId>org.glassfish.jersey.core</groupId>
  10. <artifactId>jersey-client</artifactId>
  11. <version>2.23.2</version>
  12. <scope>provided</scope>
  13. </dependency>

2.3.2 基于 Servlet 的服务端应用

以下依赖可以应用于没有集成任何 JAX-RS 实现的应用服务器(servlet 容器)。需要在部署的应用里面包含 JAX-RS API 和 Jersey 的实现。

  1. <dependency>
  2. <groupId>org.glassfish.jersey.containers</groupId>
  3. <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
  4. <artifactId>jersey-container-servlet</artifactId>
  5. <version>2.23.2</version>
  6. </dependency>
  7. <!-- 仅使用 JAX-RS Client 时添加 -->
  8. <dependency>
  9. <groupId>org.glassfish.jersey.core</groupId>
  10. <artifactId>jersey-client</artifactId>
  11. <version>2.23.2</version>
  12. </dependency>

2.3.3 运行于 JDK 的客户端应用

在 JDK 运行的应用,是否使用 JAX-RS 中客户端的规范完全取决于客户。有各种不同的附加模块可以被添加,例如像 grizzly、Apache 或 jetty 等连接器(见下面依赖)。Jersey 客户端在 JDK 默认运行(HttpUrlConnection)。更多的细节可以参见 Chapter 5, Client API

  1. <dependency>
  2. <groupId>org.glassfish.jersey.core</groupId>
  3. <artifactId>jersey-client</artifactId>
  4. <version>2.23.2</version>
  5. </dependency>

目前可用的连接器:

  1. <dependency>
  2. <groupId>org.glassfish.jersey.connectors</groupId>
  3. <artifactId>jersey-grizzly-connector</artifactId>
  4. <version>2.23.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.glassfish.jersey.connectors</groupId>
  8. <artifactId>jersey-apache-connector</artifactId>
  9. <version>2.23.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.glassfish.jersey.connectors</groupId>
  13. <artifactId>jersey-jetty-connector</artifactId>
  14. <version>2.23.2</version>
  15. </dependency>

2.3.4 服务器端应用支持的容器

除了标准的 JAX-RS 基于 Servlet 的部署(Servlet 2.5及以上版本),Jersey 对下面容器提供可编程的部署环境:Grizzly 2(HTTP 和Servlet)、JDK HTTP服务器、简单的 HTTP 服务器,Jetty HTTP 服务器。本章介绍只需要 maven 依赖,更多的内容见Chapter 4. Application Deployment and Runtime Environments 应用部署和运行时环境

  1. <dependency>
  2. <groupId>org.glassfish.jersey.containers</groupId>
  3. <artifactId>jersey-container-grizzly2-http</artifactId>
  4. <version>2.23.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.glassfish.jersey.containers</groupId>
  8. <artifactId>jersey-container-grizzly2-servlet</artifactId>
  9. <version>2.23.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.glassfish.jersey.containers</groupId>
  13. <artifactId>jersey-container-jdk-http</artifactId>
  14. <version>2.23.2</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.glassfish.jersey.containers</groupId>
  18. <artifactId>jersey-container-simple-http</artifactId>
  19. <version>2.23.2</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.glassfish.jersey.containers</groupId>
  23. <artifactId>jersey-container-jetty-http</artifactId>
  24. <version>2.23.2</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.glassfish.jersey.containers</groupId>
  28. <artifactId>jersey-container-jetty-servlet</artifactId>
  29. <version>2.23.2</version>
  30. </dependency>