转载:https://blog.csdn.net/wangshuang1631/article/details/70753801
方法一,修改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 {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//添加converter
}
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
StringHttpMessageConverter converter = new StringHttpMessageConverter( Charset.forName("UTF-8"));
}