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 接口规范;已适配的序列化插件可直接用)
  • 支持泛型
相关推荐
_UMR_22 分钟前
springboot集成Jasypt实现配置文件启动时自动解密-ENC
java·spring boot·后端
程序员小假28 分钟前
我们来说说 Cookie、Session、Token、JWT
java·后端
短剑重铸之日1 小时前
《SpringBoot4.0初识》第一篇:前瞻与思想
java·开发语言·后端·spring·springboot4.0
Jinuss1 小时前
HTML页面http-equiv=“refresh“自动刷新原理详解
http·html
蓝色王者1 小时前
springboot 2.6.13 整合flowable6.8.1
java·spring boot·后端
Tao____1 小时前
基于Ruoyi开发的IOT物联网平台
java·网络·物联网·mqtt·网络协议
花哥码天下2 小时前
apifox登录后设置token到环境变量
java·后端
浩瀚地学2 小时前
【Java】常用API(二)
java·开发语言·经验分享·笔记·学习
hashiqimiya3 小时前
springboot事务触发滚动与不滚蛋
java·spring boot·后端