网站建设与管理答案,做网站贴吧,重庆营销型网站随做的好,网页设计师证书含金量高吗TechZhi HTTP Client Starter 源码特性快速开始1. 添加依赖2. 配置3. 使用 主要功能支持的HTTP方法文件操作功能高级功能配置示例 API使用示例基本请求自定义请求异步请求文件操作示例错误处理 构建和测试依赖说明 本文将介绍一款本人开发的高性能Spring Boot HTTP客户端Starte… TechZhi HTTP Client Starter 源码特性快速开始1. 添加依赖2. 配置3. 使用 主要功能支持的HTTP方法文件操作功能高级功能配置示例 API使用示例基本请求自定义请求异步请求文件操作示例错误处理 构建和测试依赖说明 本文将介绍一款本人开发的高性能Spring Boot HTTP客户端Starter它完美整合了OkHttp和Apache HttpClient。该项目旨在解决原生HTTP客户端以及RestTemplate在文件上传下载、异步请求、失败重试等场景下的复杂性提供一个配置简单、功能强大的解决方案。
源码
https://github.com/ShouZhiDuan/easydo/blob/main/http-client-starter/README.md特性 高性能: 基于OkHttp和Apache HttpClient 5的高性能HTTP客户端 易配置: 丰富的配置选项支持连接池、超时、重试等 安全支持: 支持SSL/TLS配置可关闭证书校验 代理支持: 支持HTTP和SOCKS代理配置 日志集成: 可配置的请求/响应日志记录 重试机制: 内置智能重试机制 异步支持: 支持同步和异步HTTP请求 类型安全: 自动JSON序列化/反序列化️ Spring Boot集成: 无缝集成Spring Boot自动配置 文件操作: 完整的文件上传、下载和流处理支持 进度跟踪: 文件传输进度回调和监控 智能检测: 自动文件类型检测和Content-Type映射
快速开始
1. 添加依赖
dependencygroupIdcom.techzhi.common/groupIdartifactIdhttp-client-starter/artifactIdversion1.0.0/version
/dependency2. 配置
techzhi:http-client:enabled: trueclient-type: OK_HTTPconnect-timeout: 10sread-timeout: 30s# 更多配置选项请参考配置文档3. 使用
Service
public class MyService {Autowiredprivate HttpClient httpClient;Autowiredprivate HttpClientTemplate httpClientTemplate;public void example() {// 基本GET请求HttpResponseString response httpClient.get(https://api.example.com/data);// 带类型转换的GET请求HttpResponseUser userResponse httpClient.get(https://api.example.com/user/1, User.class);// POST请求User newUser new User(John, johnexample.com);HttpResponseUser createResponse httpClient.post(https://api.example.com/users, newUser, User.class);// 使用模板类HttpResponseString tokenResponse httpClientTemplate.getWithToken(https://api.example.com/protected, your-token);}
}主要功能
支持的HTTP方法
GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS同步和异步请求自动JSON序列化/反序列化表单提交支持
文件操作功能
文件上传: 支持单个和多个文件上传多部分表单: multipart/form-data支持可混合文件和表单字段文件下载: 支持大文件流式下载自动创建目录进度回调: 上传和下载进度实时监控文件流: 获取文件输入流和字节数组类型检测: 自动文件类型检测和Content-Type映射异步操作: 支持异步文件上传和下载
高级功能
连接池管理代理配置HTTP/SOCKSSSL/TLS配置支持关闭证书验证重试机制请求/响应日志Bearer Token和Basic认证
配置示例
techzhi:http-client:enabled: trueclient-type: OK_HTTP # OK_HTTP 或 APACHE_HTTP_CLIENTconnect-timeout: 10sread-timeout: 30swrite-timeout: 30s# 连接池配置pool:max-total: 200default-max-per-route: 50time-to-live: 5m# 代理配置proxy:enabled: falsetype: HTTPhost: proxy.example.comport: 8080# SSL配置ssl:verify-hostname: trueverify-certificate-chain: true# 重试配置retry:enabled: truemax-attempts: 3retry-interval: 1sAPI使用示例
基本请求
// GET请求
HttpResponseString response httpClient.get(https://httpbin.org/get);// POST请求
MapString, Object data Map.of(key, value);
HttpResponseString postResponse httpClient.post(https://httpbin.org/post, data);自定义请求
HttpRequest request HttpRequest.post(https://httpbin.org/post).header(Authorization, Bearer token).header(Content-Type, application/json).queryParam(page, 1).jsonBody(requestData);HttpResponseResponseData response httpClient.execute(request, ResponseData.class);异步请求
CompletableFutureHttpResponseString future httpClient.getAsync(https://httpbin.org/get);
future.thenAccept(response - {System.out.println(Response: response.getBody());
});文件操作示例
// 1. 单个文件上传
File file new File(document.pdf);
HttpResponseString uploadResponse httpClient.uploadFile(https://api.example.com/upload, file, file
);// 2. 文件上传带进度回调
FileProgressCallback progressCallback new FileProgressCallback() {Overridepublic void onProgress(long bytesTransferred, long totalBytes, double percentage) {System.out.printf(上传进度: %.2f%%\n, percentage);}Overridepublic void onComplete(long totalBytes) {System.out.println(上传完成: totalBytes bytes);}
};httpClient.uploadFile(https://api.example.com/upload, file, file, progressCallback);// 3. 多文件上传
ListMultipartFile files Arrays.asList(MultipartFile.of(file1, new File(doc1.pdf)),MultipartFile.of(file2, new File(doc2.pdf)),MultipartFile.of(data, info.json, jsonData.getBytes(), application/json)
);HttpResponseString multiResponse httpClient.uploadFiles(https://api.example.com/upload, files);// 4. 文件和表单数据混合上传
MapString, String formFields new HashMap();
formFields.put(title, 文档标题);
formFields.put(category, 技术文档);httpClient.uploadFilesWithForm(https://api.example.com/upload, files, formFields);// 5. 文件下载
boolean downloadSuccess httpClient.downloadFile(https://api.example.com/download/report.pdf, /path/to/save/report.pdf
);// 6. 文件下载带进度回调
httpClient.downloadFile(https://api.example.com/download/large-file.zip, /path/to/save/large-file.zip, progressCallback
);// 7. 获取文件字节数组
byte[] fileBytes httpClient.getFileBytes(https://api.example.com/download/small-file.txt);// 8. 获取文件流
try (InputStream fileStream httpClient.getFileStream(https://api.example.com/download/data.csv)) {// 处理文件流BufferedReader reader new BufferedReader(new InputStreamReader(fileStream));String line;while ((line reader.readLine()) ! null) {// 处理每一行}
}// 9. 异步文件操作
CompletableFutureHttpResponseString asyncUpload httpClient.uploadFileAsync(https://api.example.com/upload, file, file
);CompletableFutureBoolean asyncDownload httpClient.downloadFileAsync(https://api.example.com/download/file.zip, /path/to/save/file.zip
);错误处理
HttpResponseString response httpClient.get(https://httpbin.org/status/404);if (response.isSuccessful()) {String data response.getBody();
} else if (response.isClientError()) {System.out.println(Client error: response.getStatusCode());
} else if (response.isServerError()) {System.out.println(Server error: response.getStatusCode());
}构建和测试
# 编译项目
mvn clean compile# 运行测试
mvn test# 打包
mvn clean package# 安装到本地仓库
mvn install依赖说明
本项目支持以下HTTP客户端实现
OkHttp: 默认实现高性能、轻量级Apache HttpClient 5: 功能丰富企业级特性
根据项目需求选择合适的实现通过配置techzhi.http-client.client-type来切换。