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 接口规范;已适配的序列化插件可直接用)
  • 支持泛型
相关推荐
Flynn Liang38 分钟前
AiServices 源码级拆解:一个接口如何变成 RAG Agent
java·langchain·源码分析·rag·langchain4j·aiservices
Flynn Liang1 小时前
LangChain4j 1.23 架构全景:Java 大模型应用的 6 层抽象
java·langchain·大模型·rag·ai agent·langchain4j
聪明的笨猪猪1 小时前
实际工程问题:AI生成Redis分布式锁的原子性缺陷导致库存超卖
java·经验分享·面试·ai编程
软件技术NINI2 小时前
基于SpringBoot和Vue的新能源汽车销售推荐系统
java·vue.js·spring boot·后端·汽车·毕业设计·毕设
Flynn Liang3 小时前
LangChain4j 生产级工程骨架:12 个模块 + Docker 一键启动
java·spring boot·langchain·rag·langchain4j
Thanks_ks3 小时前
HTTP 请求方法与 RESTful 接口设计:GET 和 POST 到底怎么用
http·api·restful·状态码·get·post·接口设计
噢,我明白了4 小时前
JAVA中的一些基础应用知识点
java·开发语言
SL_staff4 小时前
私有化低代码不是妥协,而是企业级数字化的自主可控主航道
java
装不满的克莱因瓶4 小时前
【SpringAI Alibaba】ReactAgent结构化输出OutputType与OutputSchema实战
java·人工智能·ai·springai·springaialibaba
秋天的怀念_David5 小时前
算法一千零一夜 · 第二十八夜 注册
java·程序人生·算法