In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to change the coding format of RestTemplate to UTF-8". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The RestTemplate coding format is changed to UTF-8 to prevent garbled code.
When I called the API of Wechat, I found that the user data returned by Wechat did not display nicknames properly, and the nicknames were garbled.
/ / modify the encoding format of RestTemplate to UTF-8 RestTemplate restTemplate = new RestTemplate (); List text/plain Charset=UTF-8
And then automatically inject it where it is used.
Let's see why public RestTemplate () {this.messageConverters.add (new ByteArrayHttpMessageConverter ()); this.messageConverters.add (new StringHttpMessageConverter ()); this.messageConverters.add (new ResourceHttpMessageConverter ()); this.messageConverters.add (new SourceHttpMessageConverter ()); this.messageConverters.add (new AllEncompassingFormHttpMessageConverter ()); if (romePresent) {this.messageConverters.add (new AtomFeedHttpMessageConverter ()) This.messageConverters.add (new RssChannelHttpMessageConverter ());} if (jaxb2Present) {this.messageConverters.add (new Jaxb2RootElementHttpMessageConverter ());} if (jackson2Present) {this.messageConverters.add (new MappingJackson2HttpMessageConverter ());} else if (jacksonPresent) {this.messageConverters.add (new MappingJacksonHttpMessageConverter ());}}
From the RestTemplate construction method, we can see that there are several default messageConverters for restTemplate, and this time the protagonist is StringHttpMessageConverter:
Public class StringHttpMessageConverter extends AbstractHttpMessageConverter {public static final Charset DEFAULT_CHARSET = Charset.forName ("ISO-8859-1"); private final Charset defaultCharset; private final List availableCharsets; private boolean writeAcceptCharset = true; / * * A default constructor that uses {@ code "ISO-8859-1"} as the default charset. * @ see # StringHttpMessageConverter (Charset) * / public StringHttpMessageConverter () {this (DEFAULT_CHARSET);} / * * A constructor accepting a default charset to use if the requested content * type does not specify one. * / public StringHttpMessageConverter (Charset defaultCharset) {super (new MediaType ("text", "plain", defaultCharset), MediaType.ALL); this.defaultCharset = defaultCharset; this.availableCharsets = new ArrayList (Charset.availableCharsets (). Values ());} / * * Indicates whether the {@ code Accept-Charset} should be written to any outgoing request. *
Default is {@ code true}. * / public void setWriteAcceptCharset (boolean writeAcceptCharset) {this.writeAcceptCharset = writeAcceptCharset;} @ Override public boolean supports (Class clazz) {return String.class.equals (clazz);} @ Override protected String readInternal (Class)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.