Home > Back-end >  SpringBoot RestTemplate request returns the code how to solve
SpringBoot RestTemplate request returns the code how to solve

Time:09-22

 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);

HttpEntity FormEntity=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 code

CodePudding user response:

Spring RestTemplate Chinese garbled solution
Due 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:

@ Configuration
Public 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 problem

CodePudding 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. CharacterEncodingFilter 
if 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 filter

CodePudding user response:

Quote: refer to fifth floor mysterious small peak response:
where to set the coding formats, can solve???????
  • Related