Solon 3.0 新特性:HttpUtils 了解一下
Solon 3.0 引入一个叫 HttpUtils 小插件,这是一个简单的同步 HTTP 客户端,基于 URLConnection 适配(也支持切换为 OkHttp 适配)。使得编写 HTTP 客户端代码更加直观和易于阅读。
- 使用 URLConnection 适配时(大小为 40KB 左右)。默认
- 使用 OkHttp 适配时(大小为 3.1MB 左右)。当引入 okhttp 包时,自动切换为 okhttp 适配。
一、请求操作
- 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);
二、提交操作
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()); //通过临时类构建泛型(或别的方式)
三、高级操作
获取完整的响应(用完要关闭)
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);
四、总结
HttpUtils 的几个小优点:
- 简单的 API。主要就是简单!也很小巧。
- 支持自动序列化(使用了 solon serializer 接口规范;已适配的序列化插件可直接用)
- 支持泛型
Solon 3.0 新特性:HttpUtils 了解一下的更多相关文章
- 浅谈Tuple之C#4.0新特性那些事儿你还记得多少?
来源:微信公众号CodeL 今天给大家分享的内容基于前几天收到的一条留言信息,留言内容是这样的: 看了这位网友的留言相信有不少刚接触开发的童鞋们也会有同样的困惑,除了用新建类作为桥梁之外还有什么好的办 ...
- Java基础和JDK5.0新特性
Java基础 JDK5.0新特性 PS: JDK:Java Development KitsJRE: Java Runtime EvironmentJRE = JVM + ClassLibary JV ...
- Visual Studio 2015速递(1)——C#6.0新特性怎么用
系列文章 Visual Studio 2015速递(1)——C#6.0新特性怎么用 Visual Studio 2015速递(2)——提升效率和质量(VS2015核心竞争力) Visual Studi ...
- atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性
atitit.Servlet2.5 Servlet 3.0 新特性 jsp2.0 jsp2.1 jsp2.2新特性 1.1. Servlet和JSP规范版本对应关系:1 1.2. Servlet2 ...
- 背水一战 Windows 10 (1) - C# 6.0 新特性
[源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...
- C# 7.0 新特性2: 本地方法
本文参考Roslyn项目中的Issue:#259. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- C# 7.0 新特性1: 基于Tuple的“多”返回值方法
本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...
- C# 7.0 新特性3: 模式匹配
本文参考Roslyn项目Issue:#206,及Docs:#patterns. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# ...
- C# 7.0 新特性4: 返回引用
本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...
- C#发展历程以及C#6.0新特性
一.C#发展历程 下图是自己整理列出了C#每次重要更新的时间及增加的新特性,对于了解C#这些年的发展历程,对C#的认识更加全面,是有帮助的. 二.C#6.0新特性 1.字符串插值 (String In ...
随机推荐
- 【Spring-Security】Re07 持久化的记住我
Security记住我功能底层实现依赖于SpringJDBC组件,如果有持久层框架的话,就由持久层框架实现 演示案例的选型,MysqlJdbc + MybatisStarter <depende ...
- NVIDIA Omniverse Audio2Face的安装
下载 NVIDIA Omniverse 并运行安装程序 - 安装后,打开 Omniverse Launcher - 在"Apps"(应用)部分中找到 Omniverse Audio ...
- 开源机器学习版本的Github:Hugging Face
参考: https://baijiahao.baidu.com/s?id=1776478347325976510 https://zhuanlan.zhihu.com/p/535100411 ==== ...
- Ubuntu18.04 环境下 解决VScode中空格长度减小的问题
问题: Ubuntu下安装vscode后发现空格特别小,只有正常空格的一半大小 ======================================================= ...
- mujoco安装报错:mujoco_py/gl/eglplatform.h:99:10: fatal error: X11/Xlib.h: 没有那个文件或目录
安装mujoco报错: mujoco_py/gl/eglplatform.h:99:10: fatal error: X11/Xlib.h: 没有那个文件或目录 修复方法: sudo apt inst ...
- 词云图大师(WordCloudMaster)上线Web端!
我们非常激动地宣布,词云图大师(WordCloudMaster)现已正式上线Web端!这一全新版本为用户带来了更多的便捷和功能,让创建和分享词云变得更加轻松.无论是企业.教育机构还是个人用户,都可以通 ...
- 一个好用的消息推送服务【Server 酱】
今天给大家介绍一个好用的消息推送服务Server 酱 Server 酱简介 Server 酱是什么 「Server 酱」,英文名「ServerChan」,是一款「手机」和「服务器」.「智能设备」之间的 ...
- 美化一下WPF自带得ToolTip
对照一下原版和美化以后得版本 原版: ---------- 新版: 新增了 圆角 和 阴影效果; 第一步:新建项,最下面有一个自定义控件,取名为CornerToolTip. 第二步:系统会创建一个Co ...
- 批量删除git tag
批量删除远程tag $ git ls-remote -t --refs -q | awk '{print ":"$2}' | xargs git push origin To ss ...
- Win32 动态库dll
这两天学习动态库的练习,分享下方法 实例.封装窗口类的两种状态. 1.自定义窗口类QWnd 2.资源模板窗口对话框类 下面是dll的头文件,类的声明 #pragma once #ifndef _CLA ...