本项目针对http协议定义了所有http实现之上的接口来描述一个http请求,使用者只需要面对同一套语义化的接口,
如果需要切换http的实现,可以做到完全无代码修改。支持GET、POST、文件上传下载等,目前适配了以下项目:
OKHttp3、ApacheHttpComponents、HttpURLConnection、Jodd-Http
。
这一套接口基本覆盖了一个Http请求的所有参数,接口的使用也是简单的、统一的、一致的、连缀的。对URL、Header、Body、Form、文件上传提供最大的支持。
uni既作unique,是一个比较特别的项目。 又作unify,希望统一http的请求方式,统一市面上的http客户端实现。
项目的两个亮点:
SmartHttpClient
接口体系:基于Request(HttpRequest)-Response
,可以通过设置一个特定过程的组件替换默认实现SmartHttpClient
接口继承于HttpRequestHttpClient
和SimpleHttpClient
,前者提供基于HttpRequest
的请求,后者提供简单点的参数请求方式Request
支持链式调用、支持基于策略接口的Java对象转换为String、支持路径参数Response
支持基于策略接口的String转换为Java对象OkHttp3、ApacheHttpComponents、HttpURLConnection、JoddHttp
的切换JdkSmartHttpClient
全局设置,也可以针对某一个请求Request单独设置,优先级逐渐升高Config
全局配置默认参数SmartHttpClient
的beforeTemplate
和afterTemplate
方法Proxy
代理支持HttpUtil
提供的静态方法完全代理SmartHttpClient
接口,实现一句话完成Http请求下载本项目,gradle clean build得到的jar包引入工程即可。本项目依赖于utils
项目已经发布至 jcenter
、mavenCentral
(2.0.1之前)和jitpack(v2.0.2开始)
Gradle:
【mavenCentral和jcenter】
compile 'top.jfunc.network:unihttp-jdk:${version}'
compile 'top.jfunc.network:unihttp-apache:${version}'
compile 'top.jfunc.network:unihttp-okhttp3:${version}'
compile 'top.jfunc.network:unihttp-jodd:${version}'
【jitpack】
compile 'com.gitee.xxssyyyyssxx.unihttp:unihttp-jdk:${version}'
compile 'com.gitee.xxssyyyyssxx.unihttp:unihttp-apache:${version}'
compile 'com.gitee.xxssyyyyssxx.unihttp:unihttp-okhttp3:${version}'
compile 'com.gitee.xxssyyyyssxx.unihttp:unihttp-jodd:${version}'
Maven:
<!-- https://mvnrepository.com/artifact/top.jfunc.network/httpclient-jdk -->
<dependency>
<groupId>top.jfunc.network</groupId>
<artifactId>unihttp-jdk</artifactId>
<version>${version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/top.jfunc.network/httpclient-apache -->
<dependency>
<groupId>top.jfunc.network</groupId>
<artifactId>unihttp-apache</artifactId>
<version>${version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/top.jfunc.network/httpclient-okhttp3 -->
<dependency>
<groupId>top.jfunc.network</groupId>
<artifactId>unihttp-okhttp3</artifactId>
<version>${version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/top.jfunc.network/httpclient-jodd -->
<dependency>
<groupId>top.jfunc.network</groupId>
<artifactId>unihttp-jodd</artifactId>
<version>${version}</version>
</dependency>
SpringBoot环境下更简单,引入相应的starter即可,就可以直接使用SmartHttpClient的实例,当然你可以配置一些参数。
面向SmartHttpClient
@Configuration
public class HttpConfig {
@Bean("smartHttpClient")
public SmartHttpClient smartHttpClient(){
//如果要更换http的实现或者做更多的事情,可以对此bean进行配置
SmartHttpClient smartHttpClient = new JdkSmartHttpClient();
// new OkHttp3SmartHttpClient();
// new JoddSmartHttpClient();
// new ApacheSmartHttpClient(){
//重写某些方法
};
smartHttpClient.setConfig(Config.defaultConfig()...);//设置baseUrl...
retrun smartHttpClient;
}
当拿到实例之后,就可以使用接口定义的所有的方法用于http请求。
https://gitee.com/xxssyyyyssxx/unihttp/blob/master/unihttp-apache/src/test/java/top/jfunc/http/MultiRequestTest.java https://gitee.com/xxssyyyyssxx/unihttp/blob/master/unihttp-jdk/src/test/java/top/jfunc/http/MultiRequestTest.java https://gitee.com/xxssyyyyssxx/unihttp/blob/master/unihttp-jodd/src/test/java/top/jfunc/http/MultiRequestTest.java https://gitee.com/xxssyyyyssxx/unihttp/blob/master/unihttp-okhttp3/src/test/java/top/jfunc/http/MultiRequestTest.java
下面演示几种用法:
GET:
Response response = http.get(Request.of(url).setIgnoreResponseBody(false).setIncludeHeaders(true).addHeader("saleType" , "2").setResultCharset("UTF-8"));
System.out.println(response);
System.out.println("headers:" + response.getHeaders());
String s = http.get(url);
System.out.println(s);
Request request = Request.of(url).addParam("xx" , "xx").addParam("yy" , "yy").addHeader("saleType" , "2").setResultCharset("UTF-8");
byte[] bytes = http.getAsBytes(request);
System.out.println(bytes.length);
System.out.println(new String(bytes));
request = Request.of(url).setFile(new File("C:\\Users\\xiongshiyan\\Desktop\\yyyy.txt"));
File asFile = http.getAsFile(request);
System.out.println(asFile.getAbsolutePath());
POST:
Request request = Request.of(url).setIncludeHeaders(true).addHeader("ss" , "ss").addHeader("ss" , "dd").setBody("{\"name\":\"熊诗言\"}").setContentType(JSON_WITH_DEFAULT_CHARSET).setConnectionTimeout(10000).setReadTimeout(10000).setResultCharset("UTF-8");
Response post = http.post(request);
System.out.println(post.getBody());
System.out.println(post.getHeaders());
String s = http.postJson(url, "{\"name\":\"熊诗言\"}");
System.out.println(s);
request = Request.of(url).addParam("xx" , "xx").addParam("yy" , "yy").setContentType(FORM_URLENCODED);
Response response = http.post(request);
System.out.println(response.getBody());
UPLOAD:
FormFile formFile = new FormFile(new File("E:\\838586397836550106.jpg") , "filedata",null);
Request request = Request.of(url).addHeader("empCode" , "ahg0023")
.addHeader("phone" , "15208384257").addFormFile(formFile).setIncludeHeaders(true);
Response response = httpClient.upload(request);
System.out.println(response.getBody());
System.out.println(response.getHeaders());
多文件及带参数的上传:
FormFile formFile = new FormFile(new File("E:\\838586397836550106.jpg") , "filedata",null);
FormFile formFile2 = new FormFile(new File("E:\\BugReport.png") , "filedata2",null);
Request request = Request.of(url).addHeader("empCode" , "ahg0023")
.addHeader("phone" , "15208384257").addFormFile(formFile2).addFormFile(formFile).setIncludeHeaders(true);
request.addParam("k1", "v1").addParam("k2" , "v2");
Response response = httpClient.upload(request);
System.out.println(response.getBody());
System.out.println(response.getHeaders());
对SmartHttpClient设置全局默认参数
http.setConfig(Config.defaultConfig()
.setBaseUrl("https://fanyi.baidu.com/")
.addDefaultHeader("xx" , "xx")
.setDefaultBodyCharset("UTF-8")
.setDefaultResultCharset("UTF-8")
.setDefaultConnectionTimeout(15000)
.setDefaultReadTimeout(15000)
//.....
);
类似MyBatis接口使用方式
1.配置HttpService接口扫描 2.定义如下一样的接口
@Configuration
public class SomeConfiguration{
@Bean
public SmartHttpClient smartHttpClient(){
Config config = Config.defaultConfig().setBaseUrl("xxxxx);
SmartHttpClientImpl smartHttpClient = new SmartHttpClientImpl();
smartHttpClient.setConfig(config);
return smartHttpClient;
}
//以下配置可以扫描 top.jfunc.network.controller.client 包下的标注 @HttpService 注解的接口
@Bean
public HttpServiceCreator httpServiceCreator(SmartHttpClient smartHttpClient){
return new HttpServiceCreator().setSmartHttpClient(smartHttpClient);
}
@Bean
public HttpServiceScanConfigure httpServiceScanConfigure(){
HttpServiceScanConfigure httpServiceScanConfigure = new HttpServiceScanConfigure(httpServiceCreator(smartHttpClient()));
httpServiceScanConfigure.setAnnotationClassScan(HttpService.class);
httpServiceScanConfigure.setScanPackages("top.jfunc.network.controller.client");
return httpServiceScanConfigure;
}
}
@HttpService
public interface InterfaceForTestHttpService {
@GET
Response request(HttpRequest httpRequest);
@GET("/get/{q}")
Response list(@Path("q") String q, @Query("xx") int xx);
@GET("/get/query")
Response queryMap(@QueryMap Map<String, String> map);
@GET
Response url(@Url String url);
@GET("get/query")
Response header(@Header("naked") String naked);
@Headers({"xx:xiongshiyan","yy:xsy"})
@GET("get/query")
Response headers(@Header("naked") String naked);
@GET("get/query")
Response headerMap(@HeaderMap Map<String, String> map);
@GET("/get/query")
Response download();
@POST("/post/{id}")
Response post(@Path("id") String id, @Body String xx);
@Multipart
@POST("/upload/only")
Response upload(@Part FormFile... formFiles);
@Multipart
@POST("/upload/withParam")
Response uploadWithParam(@Part("name") String name, @Part("age") int age, @Part FormFile... formFiles);
@FormUrlEncoded
@POST("/post/form")
Response form(@Field("name") String name, @Field("age") int age);
@FormUrlEncoded
@POST("/post/form")
Response formMap(@FieldMap Map<String, String> params);
}
建议的最佳实践为:
更多用法等待你探索,本人才疏学浅,难免有考虑不周到的地方,请不吝赐教。
提供了服务端和客户端的测试工程,可以clone下来运行。
https://gitee.com/xxssyyyyssxx/http-server-test
https://gitee.com/xxssyyyyssxx/http-client-test
如果你想实现自己的,只需要继承 top.jfunc.http.smart.AbstractSmartHttpClient
,
参考top.jfunc.http.smart.DemoImpl
实现抽象方法即可。实现方法可以参考httpclient-jdk、apache、okhttp3、jodd等。
http模块的架构设计和使用方式见 CSDN博客
项目采用双接口并行设计模式,一种是面向接口实现者的 HttpTemplate-SmartHttpTemplate 功能接口 ,主要的功能是模拟Http的参数、header等;一种是面向终端用户调用者的 HttpClient-SmartHttpClient 用户接口 。具体实现类通过实现两组接口,后者接口实现最终调用前者的接口实现。这样做的好处是互不影响,实现者可以无限优化 HttpTemplate、SmartHttpTemplate 的实现类,而对使用者几无影响。
HttpTemplate、HttpClient接口基于方法来模拟Http的参数、header等;SmartHttpTemplate继承于HttpTemplate,SmartHttpClient接口继承于HttpClient,且基于Request来模拟Http的参数、header,这样更容易优化、更容易使用。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型