Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 28, 2025

getSandboxSignKey() 方法在 v4.7.0 版本中发送 XML 请求时未指定 Content-Type,导致默认使用 application/json,微信服务器无法解析 XML 请求体。

问题分析

v4.7.0 代码:

String responseContent = this.post(url, request.toXML(), false);

当前 develop 分支 (4.7.8.B) 已修复:

String responseContent = this.post(url, request.toXML(), false, ContentType.APPLICATION_XML.getMimeType());

结论

此问题已在 develop 分支修复。使用 v4.7.0 的用户升级到 v4.7.8+ 即可解决。

Fixes #3607

Original prompt

This section details on the original issue you should resolve

<issue_title>获取沙箱环境签名key接口报错:"mch_id" is required. "sign" is required. "nonce_str" is required</issue_title>
<issue_description>### 获取沙箱环境签名key接口报错
"mch_id" is required. "sign" is required. "nonce_str" is required
明明是有值的
`
<mch_id>3129345678</mch_id>
<nonce_str>1749114345872</nonce_str>
202524F6AF0E4AFA225F96CB34822492

`

版本环境

`
com.github.binarywang
weixin-java-pay
4.7.0

`

大概原因

`httpPost.setEntity(createEntry(requestStr));
private static StringEntity createEntry(String requestStr) {
return new StringEntity(requestStr, ContentType.create("application/json", StandardCharsets.UTF_8));
}

String url = "https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey";
String responseContent = this.post(url, request.toXML(), false);
请求接口getsignkey需要xml的格式,而post里面设置header又变成了application/json,这样传递数据就不被识别?

`

报错日志

`2025-06-05 17:05:46.107 INFO 10512 --- [nio-8080-exec-5] c.g.b.w.s.i.WxPayServiceApacheHttpImpl :
【请求地址】:https://api.mch.weixin.qq.com/xdc/apiv2getsignkey/sign/getsignkey
【请求数据】:
<mch_id>3129345678</mch_id>
<nonce_str>1749114345872</nonce_str>
202524F6AF0E4AFA225F96CB34822492

【响应数据】:
<return_code></return_code>
<return_msg></return_msg>

2025-06-05 17:05:46.122 ERROR 10512 --- [nio-8080-exec-5] c.g.b.w.b.r.WxPaySandboxSignKeyResult :
结果业务代码异常,返回结果:{return_msg="mch_id" is required. "sign" is required. "nonce_str" is required, return_code=FAIL},
返回代码:FAIL,返回信息:"mch_id" is required. "sign" is required. "nonce_str" is required
2025-06-05 17:05:46.125 ERROR 10512 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is WxPayException(customErrorMsg=null, returnCode=FAIL, returnMsg="mch_id" is required. "sign" is required. "nonce_str" is required, resultCode=null, errCode=null, errCodeDes=null, xmlString=
<return_code></return_code>
<return_msg></return_msg>
)] with root cause

com.github.binarywang.wxpay.exception.WxPayException: 返回代码:[FAIL],返回信息:["mch_id" is required. "sign" is required. "nonce_str" is required],微信返回的原始报文:

<return_code></return_code>
<return_msg></return_msg>

at com.github.binarywang.wxpay.exception.WxPayException$Builder.build(WxPayException.java:207) ~[weixin-java-pay-4.7.5-20250529.111829.jar:na]
at com.github.binarywang.wxpay.exception.WxPayException.from(WxPayException.java:99) ~[weixin-java-pay-4.7.5-20250529.111829.jar:na]
at com.github.binarywang.wxpay.bean.result.BaseWxPayResult.checkResult(BaseWxPayResult.java:392) ~[weixin-java-pay-4.7.5-20250529.111829.jar:na]
at com.github.binarywang.wxpay.service.impl.BaseWxPayServiceImpl.getSandboxSignKey(BaseWxPayServiceImpl.java:1236) ~[weixin-java-pay-4.7.5-20250529.111829.jar:na]
at com.github.binarywang.demo.wx.pay.controller.WxPayController.getSandboxSignKey(WxPayController.java:363) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.15.jar:5.3.15]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.15.jar:5.3.15]
at org.springframework.web.servlet...

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix missing parameters in sandbox signature key request 确认沙箱环境签名key接口Content-Type问题已修复 Nov 28, 2025
Copilot AI requested a review from binarywang November 28, 2025 14:03
Copilot finished work on behalf of binarywang November 28, 2025 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

获取沙箱环境签名key接口报错:"mch_id" is required. "sign" is required. "nonce_str" is required

2 participants