RestTemplate garbled problem @Arthur

      The Http request mechanism of RestTemplate has been used all the time, but it has never been deeply studied. When using its postForEntity method to send xml fragments (dynamically creating raster layers), there is a problem of garbled characters.

       Discuss with Ana and discover the following:

      1. Through the setContentType("applicaiton/json;charset=UTF-8") method of HttpHeaders, the characters are agreed. It declares the json character set sent to the server according to the requirements of the Http protocol.

       2. Under such a setting, garbled characters may still occur when using RestTemplate. The reason is that RestTemplate has a series of HTTP message converters (HttpMessageConverter). The first message converter StringHttpMessageConverter in this message converter set, its default character set is "ISO-8859-1". So when the character stream we send is UTF-8, there will be garbled characters.

 So the solution is to change the character set of StringHttpMessageConverter through restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)).

 Online reference: http://blog.csdn.net/luccs624061082/article/details/40980623

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325627714&siteId=291194637