默认情况下SpringBoot已经帮助我们解决了中文乱码问题,但是如果配置Mybatis分页插件,就会导致浏览器请求中的编码使用
ISO-8859-1
,而不是UTF-8
,导致中文乱码。
SpringBoot中配置MyBatis分页插件
@Configuration
public class MybatisConfiguration {
@Bean
public Interceptor[] interceptors(){
PageInterceptor pageInterceptor = new PageInterceptor();
return new Interceptor[]{pageInterceptor};
}
}
解决中文乱码
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
@Bean
public HttpMessageConverter<String> responseBodyConverter(){
StringHttpMessageConverter converter = new
StringHttpMessageConverter(StandardCharsets.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);
}
}