refactor(pay): 抽取 apache httpclient 支付实现的公共父类,消除重复代码 - #4082
refactor(pay): 抽取 apache httpclient 支付实现的公共父类,消除重复代码#4082devin-ai-integration[bot] wants to merge 1 commit into
Conversation
WxPayServiceApacheHttpImpl 与 WxPayServiceHttpComponentsImpl 中约 300 行 v3 请求、 请求构造、SSL/代理配置、日志与异常转换逻辑完全重复,现抽取到包内抽象父类 AbstractWxPayApacheHttpServiceImpl,两个实现类仅保留各自的 v2 post 逻辑。 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 77122707e7
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| * @param requestStr 请求数据 | ||
| * @return post 请求 | ||
| */ | ||
| protected HttpPost createHttpPost(String url, String requestStr) { |
There was a problem hiding this comment.
当下游已有 WxPayServiceApacheHttpImpl/WxPayServiceHttpComponentsImpl 子类定义同签名的 createHttpPost 时,旧实现里的这些 helper 是 private,不会参与覆盖;现在抽到父类后变成 protected,post/postV3 中的 this.createHttpPost(...) 会动态分派到用户子类,导致请求体、超时和 v3 请求头构造被意外替换,升级后行为改变。作为公共 SDK 的兼容性重构,建议把这些抽取出的内部 helper 保持包内或不可覆盖,只给两个实现类复用。
AGENTS.md reference: AGENTS.md:L47-L48
Useful? React with 👍 / 👎.
Summary
用 PMD CPD 扫描全仓库后,
weixin-java-pay中WxPayServiceApacheHttpImpl与WxPayServiceHttpComponentsImpl是逻辑层面(非 bean 字段)最大的一处重复:两个类 400+ 行中约 300 行逐字符相同,只有 v2 的postForBytes/post在获取 http 客户端的方式上不同(连接池客户端 vs 每次HttpClientBuilder.build())。任何一侧修复(如之前的Wechatpay-Serial公钥模式兼容)都必须手工同步到另一侧。本次新增包内抽象父类
AbstractWxPayApacheHttpServiceImpl extends BaseWxPayServiceImpl,收敛全部共用逻辑:行为保持不变,仅有两处细节说明:
RequestConfig.custom()...build()的重复片段提取为createRequestConfig(),参数完全一致。@Slf4j,改为protected final Logger log = LoggerFactory.getLogger(this.getClass()),因此日志分类仍然是WxPayServiceApacheHttpImpl/WxPayServiceHttpComponentsImpl(含WxPayServiceImpl),不影响既有日志配置。兼容性:两个实现类仍为 public 且仍是
BaseWxPayServiceImpl的子类,公共方法签名、WxPayServiceImpl extends WxPayServiceApacheHttpImpl及包内可见的createHttpClient(boolean)均未变;新增父类为包内可见,不扩大对外 API。HttpProxyUtils中指向createHttpClientBuilder的注释同步更新。验证
mvn -pl weixin-java-pay -am -DskipTests install编译通过。<skip>true</skip>,直接用 TestNG 运行了与本次改动相关的用例:WxPayServiceApacheHttpImplConnectionPoolTest、ConnectionPoolUsageExampleTest,5 个用例全部通过。Link to Devin session: https://app.devin.ai/sessions/65a1cece5efb4304a7e6b85aaa562d7a
Requested by: @binarywang