Solon 3.0 新特性:HttpUtils 了解一下

Solon 3.0 引入一个叫 HttpUtils 小插件,这是一个简单的同步 HTTP 客户端,基于 URLConnection 适配(也支持切换为 OkHttp 适配)。使得编写 HTTP 客户端代码更加直观和易于阅读。

  • 使用 URLConnection 适配时(大小为 40KB 左右)。默认
  • 使用 OkHttp 适配时(大小为 3.1MB 左右)。当引入 okhttp 包时,自动切换为 okhttp 适配。

一、请求操作

  • HEAD 请求并返回 status code
java 复制代码
int code = HttpUtils.http("http://localhost:8080/hello").head();
  • GET 请求并返回 body string
java 复制代码
String body = HttpUtils.http("http://localhost:8080/hello").get();
  • GET 请求并返回 body as bean
java 复制代码
//for Bean
Book book = HttpUtils.http("http://localhost:8080/book?bookId=1")
                     .getAs(Book.class);

二、提交操作

PUT、PATCH、DELETE 数据提交,与 POST 相同。

  • POST 请求并返回 body stirng (x-www-form-urlencoded)
java 复制代码
//x-www-form-urlencoded
String body = HttpUtils.http("http://localhost:8080/hello")
                       .data("name","world")
                       .post();
  • POST 请求并返回 body stirng (form-data)
java 复制代码
//form-data
String body = HttpUtils.http("http://localhost:8080/hello")
                       .data("name","world")
                       .post(true); // useMultipart
                       
                       
//form-data :: upload-file
String body = HttpUtils.http("http://localhost:8080/hello")
                       .data("name", new File("/data/demo.jpg"))
                       .post(true); // useMultipart
  • POST 请求并返回 body stirng (body-raw)
java 复制代码
//body-json
String body = HttpUtils.http("http://localhost:8080/hello")
                       .bodyOfJson("{\"name\":\"world\"}")
                       .post();
  • POST 请求并返回 body as bean (body-raw)
java 复制代码
//for Bean
Result body = HttpUtils.http("http://localhost:8080/book")
                       .bodyOfBean(book) //会通过 serializer 指定 contentType;默认为 json serializer
                       .postAs(Result.class);
                       
                       
//for Bean generic type
Result<User> body = HttpUtils.http("http://localhost:8080/book")
                       .bodyOfBean(book)
                       .postAs(new Result<User>(){}.getClass()); //通过临时类构建泛型(或别的方式)

三、高级操作

获取完整的响应(用完要关闭)

java 复制代码
try(HttpResponse resp = HttpUtils.http("http://localhost:8080/hello").data("name","world").exec("POST")) {
    int code = resp.code();
    String head = resp.header("Demo-Header");
    String body = resp.bodyAsString();
    Books body = resp.bodyAsBean(Books.class);
}

配置序列化器。默认为 json,比如改为 fury;或者自己定义。

java 复制代码
FuryBytesSerializer serializer = new FuryBytesSerializer();

Result body = HttpUtils.http("http://localhost:8080/book")
                       .serializer(serializer)
                       .bodyOfBean(book)
                       .postAs(Result.class);

四、总结

HttpUtils 的几个小优点:

  • 简单的 API。主要就是简单!也很小巧。
  • 支持自动序列化(使用了 solon serializer 接口规范;已适配的序列化插件可直接用)
  • 支持泛型
相关推荐
4277240027 分钟前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo1 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦1 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个1 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
南宫生4 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长4 小时前
Maven 基础环境搭建与配置(一)
java·maven
风与沙的较量丶5 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_748251725 小时前
SpringBoot3 升级介绍
java
极客先躯6 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行6 小时前
Spring事务原理 二
java·后端·spring