Skip to content

Commit 3f0b6cb

Browse files
authored
修复 wx-java-open-multi-spring-boot-starter 中硬编码 HttpClient4 导致的启动失败问题
- 在 WxOpenMultiProperties.ConfigStorage 中新增 HttpClientType 枚举(APACHE_HTTP / HTTP_COMPONENTS)及 httpClientType 字段,默认值为 HTTP_COMPONENTS - 修改 AbstractWxOpenConfiguration.wxOpenService() 根据 httpClientType 选择对应的 WxOpenServiceApacheHttpClientImpl 或 WxOpenServiceHttpComponentsImpl - 修改 AbstractWxOpenConfiguration.configHttp() 仅在 httpClientType 为 APACHE_HTTP 时配置 DefaultApacheHttpClientBuilder,消除对 HttpClient4 的强制依赖 Closes #4059
1 parent adb459e commit 3f0b6cb

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

spring-boot-starters/wx-java-open-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/configuration/services/AbstractWxOpenConfiguration.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
1212
import me.chanjar.weixin.open.api.WxOpenService;
1313
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
14-
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
14+
import me.chanjar.weixin.open.api.impl.WxOpenServiceApacheHttpClientImpl;
15+
import me.chanjar.weixin.open.api.impl.WxOpenServiceHttpComponentsImpl;
1516
import org.apache.commons.lang3.StringUtils;
1617

1718
import java.util.Collection;
@@ -73,7 +74,13 @@ protected WxOpenMultiServices wxOpenMultiServices(WxOpenMultiProperties wxOpenMu
7374
protected abstract WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties);
7475

7576
public WxOpenService wxOpenService(WxOpenConfigStorage configStorage, WxOpenMultiProperties wxOpenMultiProperties) {
76-
WxOpenService wxOpenService = new WxOpenServiceImpl();
77+
WxOpenMultiProperties.HttpClientType httpClientType = wxOpenMultiProperties.getConfigStorage().getHttpClientType();
78+
WxOpenService wxOpenService;
79+
if (httpClientType == WxOpenMultiProperties.HttpClientType.APACHE_HTTP) {
80+
wxOpenService = new WxOpenServiceApacheHttpClientImpl();
81+
} else {
82+
wxOpenService = new WxOpenServiceHttpComponentsImpl();
83+
}
7784
wxOpenService.setWxOpenConfigStorage(configStorage);
7885
return wxOpenService;
7986
}
@@ -137,17 +144,19 @@ private void configHttp(WxOpenInMemoryConfigStorage config, WxOpenMultiPropertie
137144
config.setRetrySleepMillis(retrySleepMillis);
138145
config.setMaxRetryTimes(maxRetryTimes);
139146

140-
// 设置自定义的HttpClient超时配置
141-
ApacheHttpClientBuilder clientBuilder = config.getApacheHttpClientBuilder();
142-
if (clientBuilder == null) {
143-
clientBuilder = DefaultApacheHttpClientBuilder.get();
144-
}
145-
if (clientBuilder instanceof DefaultApacheHttpClientBuilder) {
146-
DefaultApacheHttpClientBuilder defaultBuilder = (DefaultApacheHttpClientBuilder) clientBuilder;
147-
defaultBuilder.setConnectionTimeout(storage.getConnectionTimeout());
148-
defaultBuilder.setSoTimeout(storage.getSoTimeout());
149-
defaultBuilder.setConnectionRequestTimeout(storage.getConnectionRequestTimeout());
150-
config.setApacheHttpClientBuilder(defaultBuilder);
147+
// 仅在使用 Apache HttpClient 4.x 时配置 ApacheHttpClientBuilder 超时参数
148+
if (storage.getHttpClientType() == WxOpenMultiProperties.HttpClientType.APACHE_HTTP) {
149+
ApacheHttpClientBuilder clientBuilder = config.getApacheHttpClientBuilder();
150+
if (clientBuilder == null) {
151+
clientBuilder = DefaultApacheHttpClientBuilder.get();
152+
}
153+
if (clientBuilder instanceof DefaultApacheHttpClientBuilder) {
154+
DefaultApacheHttpClientBuilder defaultBuilder = (DefaultApacheHttpClientBuilder) clientBuilder;
155+
defaultBuilder.setConnectionTimeout(storage.getConnectionTimeout());
156+
defaultBuilder.setSoTimeout(storage.getSoTimeout());
157+
defaultBuilder.setConnectionRequestTimeout(storage.getConnectionRequestTimeout());
158+
config.setApacheHttpClientBuilder(defaultBuilder);
159+
}
151160
}
152161
}
153162
}

spring-boot-starters/wx-java-open-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/open/properties/WxOpenMultiProperties.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public static class ConfigStorage implements Serializable {
3838
*/
3939
private StorageType type = StorageType.memory;
4040

41+
/**
42+
* http客户端类型.
43+
*/
44+
private HttpClientType httpClientType = HttpClientType.HTTP_COMPONENTS;
45+
4146
/**
4247
* 指定key前缀.
4348
*/
@@ -122,4 +127,15 @@ public enum StorageType {
122127
redistemplate
123128
}
124129

130+
public enum HttpClientType {
131+
/**
132+
* Apache HttpClient 4.x
133+
*/
134+
APACHE_HTTP,
135+
/**
136+
* Apache HttpClient 5.x (HttpComponents)
137+
*/
138+
HTTP_COMPONENTS
139+
}
140+
125141
}

0 commit comments

Comments
 (0)