Spring Boot 中文亂碼問題解決方案匯總


使用 Spring Boot 開發,對外開發接口供調用,傳入參數中有中文,出現中文亂碼,查了好多資料,總結解決方法如下:

第一步,約定傳參編碼格式

不管是使用httpclient,還是okhttp,都要設置傳參的編碼,為了統一,這里全部設置為utf-8

第二步,修改application.properties文件

增加如下配置:

spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8 

此時攔截器中返回的中文已經不亂碼了,但是controller中返回的數據依舊亂碼。

第三步,修改controller的@RequestMapping

修改如下:

produces="text/plain;charset=UTF-8" 

這種方法的弊端是限定了數據類型,繼續查找資料,在stackoverflow上發現解決辦法,是在配置類中增加如下代碼:

@Configuration public class CustomMVCConfiguration extends WebMvcConfigurerAdapter { @Bean public HttpMessageConverter<String> responseBodyConverter() { StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8")); return converter; } @Override public void configureMessageConverters( List<HttpMessageConverter<?>> converters) { super.configureMessageConverters(converters); converters.add(responseBodyConverter()); } @Override public void configureContentNegotiation( ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false); } } 

便可以解決SpringBoot的中文亂碼問題了。

ps:stackoverflow網址

http://stackoverflow.com/questions/27606769/how-to-overwrite-stringhttpmessageconverter-default-charset-to-use-utf8-in-sprin

http://stackoverflow.com/questions/20935969/make-responsebody-annotated-spring-boot-mvc-controller-methods-return-utf-8

在工作中新使用springboot 然后遇到了亂碼問題 springboot 的配置文件方式和之前的srping mvc 有很大不同就讓我很是困惑,先講解我們開始使用的解決方案:
在application.properties 中增加

spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8

用來解決亂碼問題

然后發現在攔截器中返回的中文已經不亂碼了。
在后續測試中發現controller中返回的數據依舊亂碼,於是在
@RequestMapping中增加produces="text/plain;charset=UTF-8"

但是總覺得要限定了請求的數據類型,所以繼續研究,然后在查找的時候發現了HttpMessageConverter類 ,在其中的方法

protected Long getContentLength(String str, MediaType contentType) {
Charset charset = this.getContentTypeCharset(contentType);

try { return Long.valueOf((long)str.getBytes(charset.name()).length); } catch (UnsupportedEncodingException var5) { throw new IllegalStateException(var5); } 

}

private Charset getContentTypeCharset(MediaType contentType) {
return contentType != null && contentType.getCharset() != null?contentType.getCharset():this.getDefaultCharset();
}

中發現 getContentTypeCharset的MediaType是入參的數據 里面的utf-8然后在getContentLength的MediaType 的編碼是ISO-8859-1 看了下這個類中
ublic static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
所以下面的主要工作就是修改這個默認編碼
然后找到了下面兩篇文章

http://stackoverflow.com/questions/20935969/make-responsebody-annotated-spring-boot-mvc-controller-methods-return-utf-8
http://stackoverflow.com/questions/27606769/how-to-overwrite-stringhttpmessageconverter-default-charset-to-use-utf8-in-sprin

package com.kotlin.springboot.nextj2ee.config import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.converter.HttpMessageConverter import org.springframework.http.converter.StringHttpMessageConverter import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter import java.nio.charset.StandardCharsets @Configuration class CustomMVCConfiguration : WebMvcConfigurerAdapter() { @Bean fun responseBodyConverter(): HttpMessageConverter<String> { return StringHttpMessageConverter(StandardCharsets.UTF_8) } override fun configureMessageConverters( converters: MutableList<HttpMessageConverter<*>>) { super.configureMessageConverters(converters) converters.add(responseBodyConverter()) } override fun configureContentNegotiation( configurer: ContentNegotiationConfigurer) { configurer.favorPathExtension(false) } } 

完美解決了亂碼問題


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM