-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用spring restTemplate 中文乱码(APIV3) #43
Comments
#42 中提到的使用有没有效果?
我需要实验一下 |
使用过, 但是没有效果, 集成的方式和您的代码是一样的。我debug跟踪了流程, 主要原因不是在于转换器的问题。在spring |
看了下stackoverflow上答案的代码,试试先移除原有的StringHttpMessageConverter List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters.removeIf(httpMessageConverter -> httpMessageConverter instanceof StringHttpMessageConverter);
converters.add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); |
依然不行, 解析出来之后, 中文依旧乱码。我暂时先直接使用HttpClient来请求是没问题的, 集成RestTemplate就会乱码。 |
方便把整个工程打包发到我邮箱一下?注意去掉敏感信息,比如商户私钥 |
On Feb 24, 2021, at 14:21, xxoyt ***@***.***> wrote:
WechatPay2Credentials.buildMessage()方法里没有指定编码。重写该方法强制指定编码为UTF_8,应该就不乱码了
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity());
改为
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(),StandardCharsets.UTF_8);
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#43 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AHTW23QPYENXTBZ6RHMDOS3TASLHHANCNFSM4YAYCQCA>.
对的, 这个就是我一开始的解决思路, 但是我不太想使用这种侵入式的代码。所以我建议作者后面可以进行优化, 因为
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(),StandardCharsets.UTF_8);
如果后续版本这样写的话, 我不清楚会不会影响正常使用HttpClient的其他开发人员。 所以我还是建议先进行优先判断是否 getEntity里面存在了一个编码设置, 如果存在, 使用设置的编码。不存在则使用默认的,这样应该会更兼容一些。
|
可以试试在请求之前指定编码格式, |
On Feb 24, 2021, at 14:40, xxoyt ***@***.***> wrote:
可以试试在请求之前指定编码格式,
HttpHeaders headers = request.getHeaders(); ContentType contentType = ContentType.APPLICATION_JSON.withCharset(StandardCharsets.UTF_8); headers.add("Content-Type", contentType.toString());
因为默认EntityUtils.toString(final HttpEntity entity) 里的编码会从contentType里取。可以试试还未验证
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#43 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AHTW23UQI6CFR3HIGBACBODTASNOVANCNFSM4YAYCQCA>.
这种我试过, 我使用的是 MediaType.APPLICATION_JSON 到时候解析出来默认的就是
/**
* Public constant media type for {@code application/json;charset=UTF-8}.
* @deprecated as of 5.2 in favor of {@link #APPLICATION_JSON}
* since major browsers like Chrome
* <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=438464">
* now comply with the specification</a> and interpret correctly UTF-8 special
* characters without requiring a {@code charset=UTF-8} parameter.
*/
@deprecated
public static final MediaType APPLICATION_JSON_UTF8;
我debug看了一下过程, 我也不清楚为什么header里面的东西没有赋值进去 org.springframework.http.client#executeInternal
@OverRide
protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException {
addHeaders(this.httpRequest, headers);
if (this.httpRequest instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest entityEnclosingRequest = (HttpEntityEnclosingRequest) this.httpRequest;
HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity);
}
HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
return new HttpComponentsClientHttpResponse(httpResponse);
}
可以看到这个里面的HttpEntity是新构造出来的。所以尝试过在restTemplate中做很多的设置操作都不能够解决这个问题
|
是应答的数据出现乱码? |
0.2.2,使用以下的测试代码ok。乱码应该是设置
|
尝试过使用转换器进行转换, 但是是没有效果的。
可以通过更改spring的注入类方式进行实现, 但是觉得不是一个很好的实现方式。
微信这一侧默认的是UFT-8, 我认为最好的解决思路是, 更改WechatPay2Credentials.buildMessage, 将默认的方式ISO-8859-1转换为UTF-8, 避免中文乱码。
The text was updated successfully, but these errors were encountered: