gpt4 book ai didi

Solon3.0新特性:HttpUtils了解一下

转载 作者:撒哈拉 更新时间:2024-10-15 16:20:49 57 4
gpt4 key购买 nike

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

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

1、请求操作

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

2、提交操作

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

  • POST 请求并返回 body stirng (x-www-form-urlencoded)
//x-www-form-urlencoded
String body = HttpUtils.http("http://localhost:8080/hello")
                       .data("name","world")
                       .post();
  • POST 请求并返回 body stirng (form-data)
//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)
//body-json
String body = HttpUtils.http("http://localhost:8080/hello")
                       .bodyOfJson("{\"name\":\"world\"}")
                       .post();
  • POST 请求并返回 body as bean (body-raw)
//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()); //通过临时类构建泛型(或别的方式)

3、高级操作

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

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;或者自己定义.

FuryBytesSerializer serializer = new FuryBytesSerializer();

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

4、总结

HttpUtils 的几个小优点:

  • 简单的 API。主要就是简单!也很小巧。
  • 支持自动序列化(使用了 solon serializer 接口规范;已适配的序列化插件可直接用)
  • 支持泛型

最后此篇关于Solon3.0新特性:HttpUtils了解一下的文章就讲到这里了,如果你想了解更多关于Solon3.0新特性:HttpUtils了解一下的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com