Transcoding in spring resttemplate

Source: Internet
Author: User

    /** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.