/** Initialize resttemplate,resttemplate will add Httpmessageconverter * Add stringhttpmessageconverter non UTF-8 by default, so remove the original string first Httpmessageconverter, * Add one more character set to UTF-8 Stringhttpmessageconvert*/ Private voidReinitmessageconverter (resttemplate resttemplate) {List<HttpMessageConverter<?>> converterlist =resttemplate.getmessageconverters (); Httpmessageconverter<?> Convertertarget =NULL; for(httpmessageconverter<?>item:converterlist) { if(Item.getclass () = = Stringhttpmessageconverter.class) {Convertertarget=item; Break; } } if(Convertertarget! =NULL) {converterlist.remove (convertertarget); } httpmessageconverter<?> converter =NewStringhttpmessageconverter (standardcharsets.utf_8); Converterlist.add (Converter); }
Resttemplate Chinese Garbled Solution methodtime 2016-02-01 08:58:22 It Community referral information Original http://itindex.net/detail/55150-resttemplate-Chinese garbled-method themeJAXB
Source: http://www.iteye.com
Spring4.2.2.release resttemplate Chinese Garbled solution method
Resttempalate String Type default Stringhttpmessageconverter do transcoding, and Stringhttpmessageconverter's default encoding set is Iso8859-1, this project code is UTF-8 Send to the server side there will be garbled characters in Chinese
First: Set the Stringhttpmessageconverter default encoding in Resttempalate to UTF-8
Specific: Use Resttempalate constructor public resttemplate (List
Configure in the configuration file:
<BeanId="Resttemplate"class="Org.springframework.web.client.RestTemplate" > <Constructor-argindex="0" > <List> <BeanId="Bytearrayhttpmessageconverter"class="Org.springframework.http.converter.ByteArrayHttpMessageConverter" ></Bean> <BeanId="Stringhttpmessageconverter"class="Org.springframework.http.converter.StringHttpMessageConverter" > <Constructor-argValue="UTF-8" ></Constructor-arg> </Bean> <BeanId="Resourcehttpmessageconverter"class="Org.springframework.http.converter.ResourceHttpMessageConverter" ></Bean> <BeanId="Sourcehttpmessageconverter"class="Org.springframework.http.converter.xml.SourceHttpMessageConverter" ></Bean> <BeanId="Allencompassingformhttpmessageconverter"class="Org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" ></Bean> <BeanId= "Jaxb2rootelementhttpmessageconverter" class= " Org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter "></bean> <bean id= "Mappingjackson2httpmessageconverter" Class= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" >< span class= "tag" ></bean> </ list> </constructor-arg > </BEAN>
The second type: Set the content-type:application/x-www-form-urlencoded using the set HTTP request header; Charset=utf-8 Modifying the default encoding
Httpheaders headers = new Httpheaders (); MediaType type = Mediatype.parsemediatype ("application/x-www-form-urlencoded; Charset=utf-8 "); headers.setcontenttype (type); System.out.println (type); httpentity<string> requestentity = new Httpentity<string> ( Poststrutils.getpoststrfrommap (Parammap), headers); String msg = Resttemplate.postforobject (url,requestentity, string.class);
public string sendtorest (string URL, Map<String,String> Parammap) { Logger.debug ("====================start======================"); Logger.debug ("Call interface-" + resturl + URL + "Enter parameter:" + Parammap); /** * Resttempalate String Type default Stringhttpmessageconverter do transcoding, and Stringhttpmessageconverter's default encoding set is Iso8859-1, this project code is UTF-8 Send to the server side there will be garbled characters in Chinese * There are two ways to resolve this: * First: Set stringhttpmessageconverter default encoding in Resttempalate to UTF-8 * Specific: Use Resttempalate constructor public resttemplate (List<Httpmessageconverter<?>> messageconverters) Incoming stringhttpmessageconverter default encoding set is UTF-8 converter * Configured in the configuration file: *<BeanId="Resttemplate"class="Org.springframework.web.client.RestTemplate" > *<Constructor-argindex="0" > *<List> *<BeanId="Bytearrayhttpmessageconverter"class="Org.springframework.http.converter.ByteArrayHttpMessageConverter" ></Bean> *<BeanId="Stringhttpmessageconverter"class="Org.springframework.http.converter.StringHttpMessageConverter" > *<Constructor-argValue="UTF-8" ></Constructor-arg> *</Bean> *<BeanId="Resourcehttpmessageconverter"class="Org.springframework.http.converter.ResourceHttpMessageConverter" ></Bean> *<BeanId="Sourcehttpmessageconverter"class="Org.springframework.http.converter.xml.SourceHttpMessageConverter" ></Bean> *<BeanId="Allencompassingformhttpmessageconverter"class="Org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter" ></Bean> *<BeanId="Jaxb2rootelementhttpmessageconverter"class="Org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" ></Bean> *<BeanId="Mappingjackson2httpmessageconverter"class="Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" ></Bean> *</List> *</Constructor-arg> *</Bean> * * * The second type: Set the content-type:application/x-www-form-urlencoded in the HTTP request header using the settings; Charset=utf-8 Modifying the default encoding * */ There is a problem with chinese disorder//string msg = resttemplate.postforobject (Resturl + url,//poststrutils.getpoststrfrommap (PARAMMAP), String.class); // Httpheaders headers = new Httpheaders (); MediaType type = Mediatype.parsemediatype ("application/x-www-form-urlencoded; Charset=utf-8 "); Headers.setcontenttype (type); System.out.println (type); Httpentity<string> requestentity = new Httpentity<string> (Poststrutils.getpoststrfrommap (Parammap), headers); String msg = Resttemplate.postforobject (url,requestentity, String.class); Logger.debug ("Call interface-" + resturl + URL + "return:" + msg); Logger.debug ("====================end========================"); return msg;}
Transcoding in spring resttemplate