String ¶s="XXXXXX";
WechatPushMessage WechatPushMessage=new WechatPushMessage ();
WechatPushMessage. SetToInUser (" 85027490 ");
WechatPushMessage. SetContent (" test sending messages ");
RestTemplate RestTemplate=new RestTemplate ();
//restTemplate. GetMessageConverters (). The set (1, new StringHttpMessageConverter (StandardCharsets. UTF_8));
RestTemplate. GetMessageConverters (). The set (1, new StringHttpMessageConverter (Charset. Class.forname (" GBK ")));
The String url="http://XXXXX";
HttpHeaders headers=new HttpHeaders ();
MediaType type=MediaType. ParseMediaType (" application/json. Charset=utf-8 ");
Headers. SetContentType (type);
Headers. The add (" Accept ", MediaType. APPLICATION_JSON. ToString ());
JSONObject jsonObj=JSONObject. ParseObject (¶s);
HttpEntityFormEntity=new HttpEntity (jsonObj. ToString (), headers);
String result=restTemplate. PostForObject (url, formEntity, String. Class);
After the breakpoint found the return value is the code, the following figure
The code is as follows: 笀? 猀 甀 挀 挀 climbing 猀 猀?? Han 爀 甀 climbing?? 洀 climbing 猀 猀 Qiao most climbing?????? Tooth??? Made with Qiao Han Qiao?? 渀 甀 氀 氀 ji
Have this kind of situation, how to solve
CodePudding user response:
Send Chinese turn iso codeCodePudding user response:
Spring RestTemplate Chinese garbled solutionDue to the default constructor RestTemplate initialization StringHttpMessageConverter default character set is ISO - 8859-1, so cause the RestTemplate request response content will appear in Chinese gibberish, here I will be unable to ridicule, Spring. Why StringHttpMessageConverter default character set is ISO - 8859-1
CodePudding user response:
@ ConfigurationPublic class RestTemplateConfig {
@ Bean
Public RestTemplate restTemplatMessageSdk () {
HttpComponentsClientHttpRequestFactory httpRequestFactory=new HttpComponentsClientHttpRequestFactory ();
HttpRequestFactory. SetConnectionRequestTimeout (1800000);
HttpRequestFactory. SetConnectTimeout (1800000);
HttpRequestFactory. SetReadTimeout (1800000);
RestTemplate RestTemplate=new RestTemplate (httpRequestFactory);
StringHttpMessageConverter t=new StringHttpMessageConverter ();
//set to false can modify the header of the accept - charset attribute
T.s etWriteAcceptCharset (false);
T.s etDefaultCharset (StandardCharsets UTF_8);
RestTemplate. GetMessageConverters (). The add (0, t);
Return the restTemplate;
}
}
CodePudding user response:
Custom injection restTemplate set the accept - charset can solve this problemCodePudding user response:
Set the coding format, can be solved.CodePudding user response:
Spring and the container has to provide, as for as you said so trouble?CodePudding user response:
org. Springframework. Web. Filter. CharacterEncodingFilterif you use spingboot it seems that all if the default open
Apache Tomcat 8 Configuration Reference
Org. Eclipse. Jetty. Util. UrlEncoding. Charset
CodePudding user response:
This really should be resolved through the filter, and one of them is encoded filterCodePudding user response: