java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java http开发库unirest

Java中轻量级http开发库Unirest使用及实用技巧

作者:许于宝的博客

Unirest for Java 是一个轻量级、易于使用的 HTTP 客户端库,旨在简化 Java 应用程序中的 HTTP 请求发送和响应处理,本文详细介绍它的主要特性、基本用法以及一些实用技巧,感兴趣的朋友跟随小编一起看看吧

Unirest for Java 是一个轻量级、易于使用的 HTTP 客户端库,旨在简化 Java 应用程序中的 HTTP 请求发送和响应处理。它提供了简洁的链式 API,支持同步和异步请求,并且能够轻松处理 JSON 等常见数据格式。下面我会详细介绍它的主要特性、基本用法以及一些实用技巧。

下面是一个表格,汇总了 Unirest for Java 的主要特性:

特性维度说明
设计理念提供简单易用的链式 API,降低 HTTP 客户端开发复杂度
依赖体积轻量级,传统依赖方式无需外部依赖;Standalone 方式包含所有依赖
HTTP 方法支持 GET、POST、PUT、DELETE 等
请求类型支持同步和异步请求
数据格式支持方便地处理 JSON(需选择模块,如 Gson 或 Jackson)
JSON 处理从 4.x 版本开始,需显式引入 Gson 或 Jackson 等 JSON 处理模块
模块化设计采用模块化设计,例如 JSON 处理可作为独立模块引入

🔧 安装与配置

在你的 Java 项目中使用 Unirest,通常只需要通过 Maven 或 Gradle 添加依赖。

Maven 依赖
Unirest 提供了两种主要的依赖引入方式:

  1. 传统依赖:需要项目自身已包含必要的依赖项(如 Apache HTTP Client 和 JSON 处理库)。
  2. Standalone 依赖:包含了所有必须的依赖,避免了潜在依赖冲突。
<!-- 方式一:传统依赖 -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.14.1</version> <!-- 请注意版本更新 -->
</dependency>
<!-- 方式二:Standalone 依赖(推荐避免依赖冲突) -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.14.1</version>
    <classifier>standalone</classifier>
</dependency>

Gradle 依赖

// 传统依赖
implementation 'com.konghq:unirest-java:3.14.1'
// 或者 Standalone 依赖
implementation 'com.konghq:unirest-java:3.14.1:standalone'

💡 关于 4.x 版本
Unirest 的 4.x 版本采用了更模块化的设计。例如,JSON 处理功能需要单独引入特定模块(如 unirest-modules-gsonunirest-modules-jackson),并推荐使用 BOM(Bill of Materials)来管理依赖版本。建议访问 Unirest Java 官方文档GitHub 仓库 了解最新版本信息和升级指南。

🧩 核心用法

Unirest 的核心设计围绕着简洁的链式 API,让你能够流畅地构建 HTTP 请求。

1. 发送 GET 请求

GET 请求通常用于从服务器获取资源。Unirest 使得发送带参数的 GET 请求非常方便。

import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;
// 发送一个简单的 GET 请求
HttpResponse<String> response = Unirest.get("http://httpbin.org/get")
  .header("accept", "application/json") // 设置请求头
  .queryString("apiKey", "123") // 添加查询参数
  .asString(); // 指定期望的响应体类型
System.out.println(response.getStatus()); // 获取状态码
System.out.println(response.getBody()); // 获取响应体
// 查询参数可以通过多种方式添加
// 逐个添加
Unirest.get("http://localhost")
                .queryString("fruit", "apple")
                .queryString("droid", "R2D2")
                .asString();
// 请求结果为 http://localhost?fruit=apple&droid=R2D2
// 使用集合添加
Unirest.get("http://localhost")
        .queryString("fruit", Arrays.asList("apple", "orange"))
        .queryString(ImmutableMap.of("droid", "R2D2", "beatle", "Ringo"))
        .asString();
// 请求结果为 http://localhost?fruit=apple&fruit=orange&droid=R2D2&beatle=Ringo

2. 发送 POST 请求

POST 请求常用于向服务器提交数据。Unirest 支持多种方式提交数据。

// 提交表单数据
HttpResponse<String> response = Unirest.post("http://httpbin.org/post")
  .header("accept", "application/json")
  .field("param1", "value1") // 添加表单字段
  .field("param2", "value2")
  .asString();
// 提交 JSON 数据(需要相应的 JSON 处理库,如 Gson)
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://localhost/post")
      .header("accept", "application/json")
      .header("Content-Type", "application/json")
      .body("{\"name\": \"Gary\", \"age\": 30}") // 直接传递 JSON 字符串
      // 或者传递一个可序列化的 Java 对象
      // .body(new SomeUserObject("Gary")) 
      .asJson(); // 解析响应为 JsonNode
// 提交原始字符串数据
Unirest.post("http://localhost")
                .body("This is the entire body")
                .asEmpty();

3. 使用路径参数和设置基础 URL

路径参数 (Route Parameters) 可以方便地动态设置 URL 中的部分:

// 使用占位符 {fruit},并通过 routeParam 设置其值
Unirest.get("http://localhost/{fruit}")
     .routeParam("fruit", "apple") // 动态替换 {fruit} 为 apple
     .asString();
// 请求结果为 http://localhost/apple
// 所有参数值都会自动进行 URL 编码

设置基础 URL (Default Base URL) 可以简化重复的域名部分:

// 配置默认基础 URL
Unirest.config().defaultBaseUrl("http://homestar.com");
// 后续请求只需指定路径
Unirest.get("/runner").asString(); // 实际请求 http://homestar.com/runner

4. 设置请求头与身份认证

// 设置自定义请求头
Unirest.get("http://localhost")
            .header("Accept", "application/json")
            .header("x-custom-header", "hello")
            .asString();
// 基本身份认证 (Basic Authentication)
Unirest.get("http://localhost")
            .basicAuth("user", "password1!") // 自动进行 Base64 编码
            .asString();
// 此调用会添加请求头 "Authorization: Basic dXNlcjpwYXNzd29yZDEh"
// 请注意,涉及敏感信息时务必使用 HTTPS

5. 处理响应

.asXXX() 方法(如 asString, asJson)会阻塞当前线程直到收到 HTTP 响应。HttpResponse 对象包含了响应的详细信息:

HttpResponse<String> response = Unirest.get("http://httpbin.org/get").asString();
int status = response.getStatus(); // 获取状态码,例如 200
String body = response.getBody(); // 获取响应体字符串
Headers headers = response.getHeaders(); // 获取响应头信息
String contentType = response.getHeaders().getFirst("Content-Type"); // 获取指定响应头

6. 发送 PUT 和 DELETE 请求

// 发送 PUT 请求
HttpResponse<String> putResponse = Unirest.put("http://httpbin.org/put")
  .header("accept", "application/json")
  .field("param1", "value1")
  .field("param2", "value2")
  .asString();
// 发送 DELETE 请求
HttpResponse<String> deleteResponse = Unirest.delete("http://httpbin.org/delete")
  .header("accept", "application/json")
  .queryString("apiKey", "123")
  .asString();

⚙️ 高级特性与配置

异步请求

Unirest 支持异步发送请求,这对于提高应用程序的吞吐量和响应性非常有用。

// 使用 Future 处理异步请求
Future<HttpResponse<JsonNode>> future = Unirest.post("http://localhost/post")
      .header("accept", "application/json")
      .field("param1", "value1")
      .asJsonAsync(new Callback<JsonNode>() { // 指定回调函数
          @Override
          public void completed(HttpResponse<JsonNode> response) {
               // 请求成功完成时调用
               int code = response.getStatus();
               JsonNode body = response.getBody();
               Headers headers = response.getHeaders();
          }
          @Override
          public void failed(UnirestException e) {
               // 请求失败时调用
               System.out.println("The request has failed: " + e.getMessage());
          }
          @Override
          public void cancelled() {
               // 请求被取消时调用
               System.out.println("The request has been cancelled");
          }
      });
// 在未来某个时刻,你可以使用 future.get() 获取结果(这会阻塞)

文件上传

Unirest 简化了文件上传的过程。

// 分块上传大文件(示例框架,需完善)
try {
    String filePath = "path/to/large/file.zip";
    int chunkSize = 1024 * 1024; // 1MB 每块
    int totalChunks = (int) Math.ceil(new File(filePath).length() / (double) chunkSize);
    for (int chunkNumber = 0; chunkNumber < totalChunks; chunkNumber++) {
        byte[] chunkData = getChunkData(filePath, chunkNumber, chunkSize); // 需要实现读取文件块的方法
        HttpResponse<String> chunkResponse = Unirest.post("http://example.com/upload")
                .field("fileChunk", chunkData)
                .field("chunkNumber", chunkNumber)
                .field("totalChunks", totalChunks)
                .asString();
        // 处理每个分块的上传响应
    }
    // 所有分块上传完成后,通知服务器合并
    HttpResponse<String> mergeResponse = Unirest.post("http://example.com/merge")
            .field("fileName", "file.zip")
            .field("totalChunks", totalChunks)
            .asString();
} catch (Exception e) {
    e.printStackTrace();
}

配置 Unirest

Unirest 提供了一些全局配置选项,用于设置连接超时、重试策略等。

Unirest.config()
    .connectTimeout(30000) // 设置连接超时为30秒
    .enableCookieManagement(true) // 启用 Cookie 管理
    .verifySsl(false) // 是否验证 SSL 证书(生产环境应设为 true)
    .automaticRetries(true) // 启用自动重试
    .defaultBaseUrl("http://homestar.com"); // 设置默认基础 URL

// 注意:在 JavaFX 或长时间运行的应用程序中,在程序退出时关闭 Unirest 实例是一个好习惯
// Unirest.shutdown();

🧠 实践建议

  1. 资源清理:如果你的应用程序会长时间运行或多次创建 Unirest 实例,在程序退出或不再需要时,调用 Unirest.shutdown() 来释放底层连接管理器资源,避免内存泄漏。
  2. HTTPS 安全:在生产环境中,务必启用 SSL 证书验证Unirest.config().verifySsl(true)),并使用 HTTPS 协议传输敏感信息。仅在开发测试时考虑禁用 SSL 验证。
  3. 异常处理:网络请求充满不确定性,务必使用 try-catch 块妥善处理 UnirestException 等异常,并根据需要检查 HTTP 状态码。
  4. 依赖管理
    • 对于新项目,建议评估使用 4.x 版本,并根据需要引入 JSON 模块。
    • 使用 standalone 分类器可以减少依赖冲突的可能性。
    • 如果项目本身已管理了 HTTP 客户端和 JSON 库,则可使用传统依赖方式。
  5. JSON 处理:在 3.x 版本中,Unirest 通常默认集成 Gson。在 4.x 版本中,你需要明确选择并引入 unirest-modules-gsonunirest-modules-jackson 等模块。

📚 总结

Unirest for Java 通过其链式 API 和合理的默认配置,显著降低了在 Java 应用中处理 HTTP 请求的复杂度。无论是简单的 REST API 调用还是复杂的文件上传场景,它都能提供清晰、灵活的解决方案。

选择 Unirest,意味着你在追求开发效率和应用性能之间找到一个不错的平衡点。当然,对于极度追求性能或者需要精细控制 HTTP 行为的场景,你可能仍需考虑更底层的库(如 Apache HttpClient 或 OkHttp)。但对于大多数日常开发任务而言,Unirest 无疑是一个“灰常”优秀的工具。

到此这篇关于Java中轻量级http开发库Unirest使用及实用技巧的文章就介绍到这了,更多相关java http开发库unirest内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文