🔥 HttpUtils 详解

一、详解

1.1 介绍

现如今的 Web 项目,由服务端向外发起网络请求的场景,基本上随处可见! 传统情况下,在服务端代码里访问 http 服务时,一般会使用 JDK 的 HttpURLConnection 或者 Apache 的 HttpClient,不过这种方法使用起来太过繁琐,而且 api 使用起来非常的复杂,还得操心资源回收。

1.2 什么是 HttpUtils?

  • HttpUtils 是 Solon 提供的进行远程调用客户端
  • HttpUtils 提供了很多远程调用的方法,能够大大提高客户端的编写效率。 HttpUtils 接口实现了 HttpURLConnection 的适配(默认),以及 OkHttp 的适配。
  • 官网地址: solon-net-httputils

1.3 引入依赖

<dependency>
    <groupId>org.noear</groupId>
    <artifactId>solon-net-httputils</artifactId>
</dependency>

HttpUtils 不需要初始化,即可直接使用。而且,可以直接使用负载均衡的能力(需要引入 solon-cloud 的插件,提供底层支持)。像这样:

HttpUtils.http("user-service", "/user/get?id=1").get();

二、接口使用

HttpUtils 最大的特色就是对各种网络请求方式做了包装,能极大的简化开发人员的工作量,下面我们以 GET、POST、PUT、DELETE、文件上传与下载为例,分别介绍各个API的使用方式。

2.1 GET 请求

通过 HttpUtils 发送 HTTP GET 协议请求,经常使用到的方法有两个:

  • get() -> String
  • getAs(Type type) -> T (支持泛型)

在 Solon 环境下写一个单元测试用例,首先创建一个 Api 接口,然后编写单元测试进行服务测试。

不带参的get请求

@Controller
public class TestController {
    @Get
    @Mapping("testGet")
    public Result testGet(){
        Result result = new Result();
        result.setCode("200");
        result.setMsg("demo...");
        return result;
    }
}

@Data
public class Result {
    private String code;
    private String msg;
}

单元测试(不带参的get请求)

@Test
public void testGet(){
    //请求地址
    String url = "http://localhost:8080/testGet";
 
    //发起请求,直接返回对象
    Result result = HttpUtils.http(url).getAs(Result.class);
    System.out.println(result.toString());

带参的get请求(使用占位符号传参)

@Controller
public class TestController {
    @Get
    @Mapping("testGetByRestFul/{id}/{name}")
    public Result testGetByRestFul(@Path("id") String id, @Path("name") String name){
        Result result = new Result();
        result.setCode("200");
        result.setMsg("demo...");
        return result;
    }
}

单元测试(带参的get请求),顺带加了个 header 信息。

@Test
public void testGetByRestFul(){
    //请求地址
    String url = "http://localhost:8080/testGetByRestFul/001/张三";
 
    //发起请求,直接返回对象(restful风格)
    Result result = HttpUtils.http(url).header("App-Id","1").getAs(Result.class);
    System.out.println(result.toString());
}

2.2 POST 请求

其实 POST 请求方法和 GET 请求方法上大同小异,HttpUtils 的 POST 请求也包含两个主要方法:

  • post() -> String
  • postAs(Type type) -> T(支持泛型)

模拟表单请求,post方法测试

@Controller
public class TestController {
    @Post
    @Mapping("testPostByForm")
    public Result testPostByForm(String userName, String userPwd){
        Result result = new Result();
        result.setCode("200");
        result.setMsg("Demo...");
        return result;
    }
}

x-www-form-urlencoded post

@Test
public void testPostByForm(){
    //请求地址
    String url = "http://localhost:8080/testPostByForm";
 
    //发起请求
    Result result = HttpUtils.http(url)
                             .data("userName", "唐三藏")
                             .data("userPwd", "123456")
                             .postAs(Result.class);
                  
    System.out.println(result.toString());
}

form-data post,顺带加上文件上传

@Test
public void testPostByForm(){
    //请求地址
    String url = "http://localhost:8080/testPostByForm";
 
    //发起请求
    Result result = HttpUtils.http(url)
                             .data("userName", "唐三藏")
                             .data("userPwd", "123456")
                             .data("file", "logo.jpg", new File("/data/logo.jpg")) 
                             .postAs(Result.class, true); //useMultipart = true
                  
    System.out.println(result.toString());
}

json-body post

@Test
public void testPostByForm(){
    //请求地址
    String url = "http://localhost:8080/testPostByForm";
 
    //发起请求
    Result result = HttpUtils.http(url)
                             .bodyOfJson("{\"userName\":\"唐三藏\",\"userPwd\":\"123456\"}")
                             .postAs(Result.class); 
                  
    System.out.println(result.toString());
}

bean-body post

@Test
public void testPostByForm(){
    //请求地址
    String url = "http://localhost:8080/testPostByForm";
    
    UserBean user = new UserBean();
    user.setUserName("唐三藏");
    user.setUserPwd("123456")
 
    //发起请求
    Result result = HttpUtils.http(url)
                             .bodyOfBean(user)
                             .postAs(Result.class); 
                  
    System.out.println(result.toString());
}

2.3 PUT、PATCH、DELETE 请求

用法与 POST 完全相同。

2.4 高级用法

获取响应(用完要关闭)

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();
}

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

FuryBytesSerializer serializer = new FuryBytesSerializer();

Result body = HttpUtils.http("http://localhost:8080/book")
                       .serializer(serializer)
                       .bodyOfBean(book)
                       .postAs(Result.class);
优秀的个人博客,低调大师

微信关注我们

原文链接:https://www.oschina.net/news/316509

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
优质分享Android(本站安卓app)

优质分享Android(本站安卓app)

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。