Get the App
SLTechnology News&Howtos  ›  Development  › 

How to change the coding format of RestTemplate to UTF-8

Shulou Source: shulou.com Published: 2022-06-02 22:18:32 10月17日 Update

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)

Tags: Garbled coding UTF-8 format method Chinese configuration content nickname more knowledge problem practical concise successful next protagonist code dilemma place Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info macOS OPPO Reno Xiaomi