Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,45 @@
- **微信开放平台**`weixin-java-open`)主要用于第三方平台,代公众号或小程序进行开发和管理


---------------------------------
### HTTP 客户端支持

本项目同时支持多种 HTTP 客户端实现,默认推荐使用 **Apache HttpClient 5.x**(最新稳定版本)。

#### 支持的 HTTP 客户端类型

| HTTP 客户端 | 说明 | 配置值 | 推荐程度 |
|------------|------|--------|---------|
| Apache HttpClient 5.x | Apache HttpComponents Client 5.x,最新版本 | `HttpComponents` | ⭐⭐⭐⭐⭐ 推荐 |
| Apache HttpClient 4.x | Apache HttpClient 4.x,向后兼容 | `HttpClient` | ⭐⭐⭐⭐ 兼容 |
| OkHttp | Square OkHttp 客户端 | `OkHttp` | ⭐⭐⭐ 可选 |
| Jodd-http | Jodd 轻量级 HTTP 客户端 | `JoddHttp` | ⭐⭐ 可选 |

#### 配置方式

**Spring Boot 配置示例:**

```properties
# 使用 HttpClient 5.x(推荐,MP/CP/Channel/QiDian 模块默认)
wx.mp.config-storage.http-client-type=HttpComponents

# 使用 HttpClient 4.x(兼容模式,MiniApp 模块默认)
wx.mp.config-storage.http-client-type=HttpClient

# 使用 OkHttp
wx.mp.config-storage.http-client-type=OkHttp

# 使用 Jodd-http
wx.mp.config-storage.http-client-type=JoddHttp
```
Comment on lines +135 to +147
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README 配置示例中使用了 HttpComponents(PascalCase),这与部分模块的枚举命名一致,但与 multi-spring-boot-starter 模块中的 HTTP_COMPONENTS(UPPER_SNAKE_CASE)命名不一致。

这可能会让用户在配置 multi-starter 时感到困惑,因为文档示例使用的是单体 starter 的命名风格。

建议在文档中明确说明:

  1. 单体 starter 使用 HttpComponents
  2. Multi-starter 使用 HTTP_COMPONENTS
    或者统一两种 starter 的枚举命名风格。

Copilot uses AI. Check for mistakes.

**注意事项:**
1. **MiniApp 模块**目前暂不支持 HttpClient 5.x,默认使用 HttpClient 4.x
2. **MP、CP、Channel、QiDian 模块**已支持 HttpClient 5.x,默认推荐使用
Comment on lines +150 to +151
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文档说明「MiniApp 模块目前暂不支持 HttpClient 5.x,默认使用 HttpClient 4.x」与代码不一致。虽然 WxMaProperties 默认使用 HttpClient(4.x),但在多个 HttpClientType 枚举中已经添加了 HttpComponents 选项(如 wx-java-miniapp-spring-boot-starterwx-java-miniapp-solon-plugin)。

问题:

  1. 如果 MiniApp 模块真的不支持 HttpClient 5.x,那么不应该在枚举中添加 HttpComponents 选项
  2. 如果已经添加了枚举选项,说明已经有部分支持,文档描述应该更准确

建议明确 MiniApp 模块对 HttpClient 5.x 的支持情况。

Suggested change
1. **MiniApp 模块**目前暂不支持 HttpClient 5.x,默认使用 HttpClient 4.x
2. **MP、CP、Channel、QiDian 模块**已支持 HttpClient 5.x,默认推荐使用
1. **MiniApp 模块**已提供 `HttpComponents`HttpClient 5.x)类型的配置选项,但当前默认仍为 HttpClient 4.x;如需启用 HttpClient 5.x,请确保所使用的集成模块(如 `wx-java-miniapp-spring-boot-starter``wx-java-miniapp-solon-plugin`)版本已支持该选项
2. **MP、CP、Channel、QiDian 模块**已完整支持 HttpClient 5.x,默认推荐使用

Copilot uses AI. Check for mistakes.
3. 如需使用 OkHttp 或 Jodd-http,需在项目中添加对应的依赖(scope为provided)
Comment on lines +151 to +152
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文档中描述「MP、CP、Channel、QiDian 模块已支持 HttpClient 5.x」,但实际上 PR 中没有修改 CP(企业微信)单体 starter 模块的 WxCpProperties

检查发现 wx-java-cp-spring-boot-starter 模块的 WxCpProperties 类中没有 httpClientType 字段和 HttpClientType 枚举的定义(与其他模块如 MP、QiDian 不同)。这意味着该模块可能尚未完整支持多种 HTTP 客户端类型的配置切换。

建议验证 CP 模块是否真正支持 HttpClient 5.x 配置,如果不支持,应从文档描述中移除。

Copilot uses AI. Check for mistakes.
4. HttpClient 4.x 和 HttpClient 5.x 可以共存,按需配置即可


---------------------------------
### 版本说明

Expand Down
201 changes: 201 additions & 0 deletions docs/HTTPCLIENT_UPGRADE_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# HttpClient 升级指南

## 概述

从 WxJava 4.7.9.B 版本开始,项目开始支持并推荐使用 **Apache HttpClient 5.x**(HttpComponents Client 5),同时保持对 HttpClient 4.x 的向后兼容。

## 为什么升级?

1. **Apache HttpClient 5.x 是最新稳定版本**:提供更好的性能和更多的功能
2. **HttpClient 4.x 已经进入维护模式**:不再积极开发新功能
3. **更好的安全性**:HttpClient 5.x 包含最新的安全更新和改进
4. **向前兼容**:为未来的开发做好准备

## 支持的 HTTP 客户端

| HTTP 客户端 | 版本 | 配置值 | 状态 | 说明 |
|------------|------|--------|------|------|
| Apache HttpClient 5.x | 5.5 | `HttpComponents` | ⭐ 推荐 | 最新稳定版本 |
| Apache HttpClient 4.x | 4.5.13 | `HttpClient` | ✅ 支持 | 向后兼容 |
| OkHttp | 4.12.0 | `OkHttp` | ✅ 支持 | 需自行添加依赖 |
| Jodd-http | 6.3.0 | `JoddHttp` | ✅ 支持 | 需自行添加依赖 |

## 模块支持情况

| 模块 | HttpClient 5.x 支持 | 默认客户端 |
|------|-------------------|-----------|
| weixin-java-mp(公众号) | ✅ 是 | HttpComponents (5.x) |
| weixin-java-cp(企业微信) | ✅ 是 | HttpComponents (5.x) |
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文档中声称「weixin-java-cp(企业微信)」支持 HttpClient 5.x 且默认使用 HttpComponents (5.x),但根据代码检查,wx-java-cp-spring-boot-starter 模块的 WxCpProperties 类中没有 httpClientType 字段和 HttpClientType 枚举。

这意味着:

  1. CP 单体 starter 可能不支持通过配置切换 HTTP 客户端类型
  2. 或者该模块使用不同的配置方式

建议核实 CP 模块的实际支持情况,确保文档与代码实现一致。

Suggested change
| weixin-java-cp(企业微信) | ✅ 是 | HttpComponents (5.x) |
| weixin-java-cp(企业微信) | ⚠️ 视集成方式而定 | 参考对应 starter 配置 |

Copilot uses AI. Check for mistakes.
| weixin-java-channel(视频号) | ✅ 是 | HttpComponents (5.x) |
| weixin-java-qidian(企点) | ✅ 是 | HttpComponents (5.x) |
| weixin-java-miniapp(小程序) | ⚠️ 否 | HttpClient (4.x) |
| weixin-java-pay(支付) | ✅ 是 | HttpComponents (5.x) |
| weixin-java-open(开放平台) | ✅ 是 | HttpComponents (5.x) |

**注意**:weixin-java-miniapp 模块目前尚未实现 HttpClient 5.x 的支持,将在未来版本中添加。

## 对现有项目的影响

### 对新项目
- **无需任何修改**,直接使用最新版本即可
- 支持 HttpClient 5.x 的模块会自动使用 HttpComponents (5.x)

### 对现有项目
- **向后兼容**:不需要修改任何代码
- HttpClient 4.x 依然可用,项目同时包含两个版本的依赖
- 如果希望继续使用 HttpClient 4.x,只需在配置中显式指定

## 迁移步骤

### 1. 更新 WxJava 版本

`pom.xml` 中更新版本:

```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.7.9.B</version> <!-- 或更高版本 -->
</dependency>
Comment on lines +58 to +59
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

迁移指南中的版本号示例使用了 4.7.9.B,但 README.md 文档开头显示当前最新正式版本是 4.7.0

建议:

  1. 如果 4.7.9.B 是计划发布的版本号,应该在文档中明确说明
  2. 或者使用占位符如 4.7.x最新版本 来避免混淆

Copilot uses AI. Check for mistakes.
```

### 2. 检查配置(可选)

#### Spring Boot 项目

`application.properties``application.yml` 中:

```properties
# 使用 HttpClient 5.x(推荐,无需配置,已经是默认值)
wx.mp.config-storage.http-client-type=HttpComponents

# 或者继续使用 HttpClient 4.x
wx.mp.config-storage.http-client-type=HttpClient
```

#### 纯 Java 项目

```java
// 使用 HttpClient 5.x(推荐)
WxMpService wxMpService = new WxMpServiceHttpComponentsImpl();

// 或者继续使用 HttpClient 4.x
WxMpService wxMpService = new WxMpServiceHttpClientImpl();
```

### 3. 测试应用

升级后,建议进行全面测试以确保一切正常工作。

## 常见问题

### Q: 升级后会不会破坏现有代码?
A: 不会。项目保持完全向后兼容,HttpClient 4.x 的所有实现都保持不变。

### Q: 我需要修改代码吗?
A: 大多数情况下不需要。如果希望继续使用 HttpClient 4.x,只需在配置中指定 `http-client-type=HttpClient` 即可。

### Q: 为什么 MiniApp 模块不支持 HttpClient 5.x?
A: MiniApp 模块的 HttpClient 5.x 实现正在开发中,将在未来版本中提供。目前 MiniApp 模块会继续使用 HttpClient 4.x。

### Q: 我可以在同一个项目中同时使用两个版本吗?
A: 可以。不同的模块可以配置使用不同的 HTTP 客户端。例如,MP 模块使用 HttpClient 5.x,MiniApp 模块使用 HttpClient 4.x。
Comment on lines +99 to +102
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

迁移指南提到「MiniApp 模块的 HttpClient 5.x 实现正在开发中」,但在代码变更中已经为 MiniApp 的多个 HttpClientType 枚举添加了 HttpComponents 选项。

这种不一致可能导致用户困惑:

  1. 如果 MiniApp 真的不支持 5.x,不应该在枚举中提供该选项
  2. 如果已经部分支持(例如核心实现已完成但 starter 配置未完成),文档应该更准确地说明

建议明确说明当前的支持状态和限制。

Suggested change
A: MiniApp 模块的 HttpClient 5.x 实现正在开发中,将在未来版本中提供。目前 MiniApp 模块会继续使用 HttpClient 4.x
### Q: 我可以在同一个项目中同时使用两个版本吗?
A: 可以。不同的模块可以配置使用不同的 HTTP 客户端。例如,MP 模块使用 HttpClient 5.x,MiniApp 模块使用 HttpClient 4.x。
A: MiniApp 模块在核心 SDK 中已经提供了基于 HttpClient 5.x`HttpComponents`)的支持,但默认仍会使用 HttpClient 4.x(`HttpClient`)。如果你使用的是框架集成(例如 Spring Boot Starter),相关自动配置可能暂未默认切换到 5.x,此时可以通过显式配置 `http-client-type=HttpComponents` 或关注后续版本说明
### Q: 我可以在同一个项目中同时使用两个版本吗?
A: 可以。不同的模块可以配置使用不同的 HTTP 客户端。例如,MP 模块使用 HttpClient 5.x,MiniApp 模块默认使用 HttpClient 4.x,但也可以按需配置为 HttpClient 5.x。

Copilot uses AI. Check for mistakes.

### Q: 如何排除不需要的依赖?
A: 如果只想使用一个版本,可以在 `pom.xml` 中排除另一个:

```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.7.9.B</version>
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

迁移指南中再次提到版本号 4.7.9.B,与之前的问题相同。为了文档的准确性和可维护性,建议使用更通用的版本号表示方式。

Copilot uses AI. Check for mistakes.
<exclusions>
<!-- 排除 HttpClient 4.x -->
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</exclusion>
</exclusions>
</dependency>
```

## 配置参考

### Spring Boot 完整配置示例

```properties
# 公众号配置
wx.mp.app-id=your_app_id
wx.mp.secret=your_secret
wx.mp.token=your_token
wx.mp.aes-key=your_aes_key

# HTTP 客户端配置
wx.mp.config-storage.http-client-type=HttpComponents # HttpComponents, HttpClient, OkHttp, JoddHttp

# HTTP 代理配置(可选)
wx.mp.config-storage.http-proxy-host=proxy.example.com
wx.mp.config-storage.http-proxy-port=8080
wx.mp.config-storage.http-proxy-username=proxy_user
wx.mp.config-storage.http-proxy-password=proxy_pass

# 超时配置(可选)
wx.mp.config-storage.connection-timeout=5000
wx.mp.config-storage.so-timeout=5000
wx.mp.config-storage.connection-request-timeout=5000
```

## 技术细节

### HttpClient 4.x 与 5.x 的主要区别

1. **包名变更**
- HttpClient 4.x: `org.apache.http.*`
- HttpClient 5.x: `org.apache.hc.client5.*`, `org.apache.hc.core5.*`

2. **API 改进**
- HttpClient 5.x 提供更现代的 API 设计
- 更好的异步支持
- 改进的连接池管理

3. **性能优化**
- HttpClient 5.x 包含多项性能优化
- 更好的资源管理

### 项目中的实现

WxJava 项目通过策略模式支持多种 HTTP 客户端:

```
weixin-java-common/
├── util/http/
│ ├── apache/ # HttpClient 4.x 实现
│ ├── hc/ # HttpClient 5.x (HttpComponents) 实现
│ ├── okhttp/ # OkHttp 实现
│ └── jodd/ # Jodd-http 实现
```

每个模块都有对应的 Service 实现类:
- `*ServiceHttpClientImpl` - 使用 HttpClient 4.x
- `*ServiceHttpComponentsImpl` - 使用 HttpClient 5.x
- `*ServiceOkHttpImpl` - 使用 OkHttp
- `*ServiceJoddHttpImpl` - 使用 Jodd-http

## 反馈与支持

如果在升级过程中遇到问题,请:

1. 查看 [项目 Wiki](https://github.com/binarywang/WxJava/wiki)
2.[GitHub Issues](https://github.com/binarywang/WxJava/issues) 中搜索或提交问题
3. 加入技术交流群(见 README.md)

## 总结

-**推荐使用 HttpClient 5.x**:性能更好,功能更强
-**向后兼容**:可以继续使用 HttpClient 4.x
-**灵活配置**:支持多种 HTTP 客户端,按需选择
-**平滑迁移**:无需修改代码,仅需配置即可
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<httpclient.version>4.5.13</httpclient.version>
<httpclient5.version>5.5</httpclient5.version>
<jetty.version>9.4.57.v20241219</jetty.version> <!-- 这个不能用10以上的版本,不支持jdk8-->
</properties>
<dependencyManagement>
Expand All @@ -157,13 +158,14 @@
<version>4.12.0</version>
<scope>provided</scope>
</dependency>
<!-- HttpClient 5.x - 默认依赖(推荐使用) -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.5</version>
<scope>provided</scope>
<version>${httpclient5.version}</version>
</dependency>
Comment on lines +161 to 166
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从 pom.xml 的变更可以看出,httpclient5 的 scope 从 provided 变更为默认(编译依赖),这意味着 HttpClient 5.x 现在会被传递依赖到使用 WxJava 的项目中。

这是一个重要的依赖策略变化。虽然 PR 描述说明这是有意为之(设为默认),但需要考虑:

  1. 现在项目同时依赖 HttpClient 4.x 和 5.x,可能会增加 jar 包体积
  2. 如果用户的项目中已经依赖了不同版本的 HttpClient,可能会产生版本冲突

建议:

  1. 在发布说明中明确提及这一变更
  2. 在文档中提供排除不需要的 HTTP 客户端依赖的示例(已经在迁移指南中提供,很好)

Copilot uses AI. Check for mistakes.

<!-- HttpClient 4.x - 默认依赖(为了保持向后兼容) -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/
public enum HttpClientType {
/**
* HttpClient
* HttpClient (Apache HttpClient 4.x)
*/
HttpClient
HttpClient,
/**
* HttpComponents (Apache HttpClient 5.x)
*/
HttpComponents,
Comment on lines 9 to +16
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Channel Solon 插件的注释风格与 Spring Boot starter 一致,存在同样的问题。建议统一所有枚举值的注释详细程度。

Copilot uses AI. Check for mistakes.
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static class ConfigStorage {
/**
* http客户端类型
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;

/**
* http代理主机
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents (Apache HttpClient 5.x).
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MiniApp Solon 插件同样存在注释风格不一致的问题。虽然添加了 HttpComponents 枚举值,但根据文档声称 MiniApp 不支持 HttpClient 5.x。这种不一致需要解决。

Suggested change
* HttpComponents (Apache HttpClient 5.x).
* HttpComponents.

Copilot uses AI. Check for mistakes.
*/
HttpComponents,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents (Apache HttpClient 5.x).
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solon MP 插件中的注释风格与其他模块类似,存在注释详细程度不一致的问题。建议统一所有枚举值的注释风格。

Suggested change
* HttpComponents (Apache HttpClient 5.x).
* HttpComponents.

Copilot uses AI. Check for mistakes.
*/
HttpComponents,
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static class ConfigStorage implements Serializable {
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;

/**
* http代理主机.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents (Apache HttpClient 5.x).
*/
HttpComponents,
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static class ConfigStorage implements Serializable {
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;

/**
* http代理主机.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public enum HttpClientType {
// * OkHttp.
// */
// OK_HTTP,
/**
* HttpComponents (Apache HttpClient 5.x).
*/
HttpComponents,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/
public enum HttpClientType {
/**
* HttpClient
* HttpClient (Apache HttpClient 4.x)
*/
HttpClient
HttpClient,
/**
* HttpComponents (Apache HttpClient 5.x)
*/
HttpComponents,
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static class ConfigStorage {
/**
* http客户端类型
*/
private HttpClientType httpClientType = HttpClientType.HttpClient;
private HttpClientType httpClientType = HttpClientType.HttpComponents;

/**
* http代理主机
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public enum HttpClientType {
/**
* HttpClient.
* HttpClient (Apache HttpClient 4.x).
*/
HttpClient,
/**
Expand All @@ -19,4 +19,8 @@ public enum HttpClientType {
* JoddHttp.
*/
JoddHttp,
/**
* HttpComponents (Apache HttpClient 5.x).
*/
HttpComponents,
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#wx.mp.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379
#wx.mp.config-storage.redis.sentinel-name=mymaster
# http客户端配置
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
wx.mp.config-storage.http-client-type=HttpComponents # http客户端类型: HttpComponents(Apache HttpClient 5.x,推荐), HttpClient(Apache HttpClient 4.x), OkHttp, JoddHttp
wx.mp.config-storage.http-proxy-host=
wx.mp.config-storage.http-proxy-port=
wx.mp.config-storage.http-proxy-username=
Expand Down
Loading