springboot配置字元轉換器以準備應對讀寫字元亂碼的重要問題,配置的方法如下
1、在application.properties中設定
#編碼格式 spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
2、自己手寫編碼過濾器
//字元編碼過濾器
@WebFilter(urlPatterns = "/*",filterName = "CharacterEncodingFilter")
public class CharacterEncodingFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
filterChain.doFilter(request , response);
public void destroy() {
3、使用java配置寫一個字元編碼配置類
/**
* 中文亂碼解決
*/
@Configuration
public class CharsetConfig extends WebMvcConfigurerAdapter {
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter(
Charset.forName("UTF-8"));
return converter;
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
converters.add(responseBodyConverter());
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
StringHttpMessageConverter是一個請求和響應資訊的編碼轉換器,透過原始碼我們發現預設編碼ISO-8859-1,不是UTF-8,所以我們只要透過上述配置將請求字串轉為UTF-8 即可。
WebMvcConfigurerAdapter 是springmvc的一個配置支配器類,我們可以實現我們感興趣的方法。
springboot配置字元轉換器以準備應對讀寫字元亂碼的重要問題,配置的方法如下
1、在application.properties中設定
#編碼格式 spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true server.tomcat.uri-encoding=UTF-8
2、自己手寫編碼過濾器
//字元編碼過濾器
@WebFilter(urlPatterns = "/*",filterName = "CharacterEncodingFilter")
public class CharacterEncodingFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
filterChain.doFilter(request , response);
}
@Override
public void destroy() {
}
}
3、使用java配置寫一個字元編碼配置類
/**
* 中文亂碼解決
*/
@Configuration
public class CharsetConfig 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);
}
}
StringHttpMessageConverter是一個請求和響應資訊的編碼轉換器,透過原始碼我們發現預設編碼ISO-8859-1,不是UTF-8,所以我們只要透過上述配置將請求字串轉為UTF-8 即可。
WebMvcConfigurerAdapter 是springmvc的一個配置支配器類,我們可以實現我們感興趣的方法。