在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法。

这里自己做个汇总。

在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"

但是这样就得要限定请求的数据类型,并且需要在每个请求里都加上这个,工作比较繁杂。

可以自己写一个配置文件类,继承WebMvcConfigurationSupport,代码如下:

 @Configuration
public class CustomMVCConfiguration extends WebMvcConfigurationSupport{ @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);
}
}

基本上就解决了乱码问题。

如果出现org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap

就需要在配置类中,添加一些代码,完整的代码如下:

 @Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport { /**
* 解决中文乱码问题
*/
@Bean
public HttpMessageConverter<String> responseBodyConverter() {
return new StringHttpMessageConverter(Charset.forName("UTF-8"));
} @Bean
public ObjectMapper getObjectMapper() {
return new ObjectMapper();
} @Bean
public MappingJackson2HttpMessageConverter messageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(getObjectMapper());
return converter;
} @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
//解决中文乱码
converters.add(responseBodyConverter());
//解决 添加解决中文乱码后 上述配置之后,返回json数据直接报错 500:no convertter for return value of type
converters.add(messageConverter());
converters.add(responseBodyConverter());
} @Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}

spring boot 中文乱码问题的更多相关文章

  1. Spring Boot 中文乱码问题解决方案汇总

    使用 Spring Boot 开发,对外开发接口供调用,传入参数中有中文,出现中文乱码,查了好多资料,总结解决方法如下: 第一步,约定传参编码格式 不管是使用httpclient,还是okhttp,都 ...

  2. 解决spring boot中文乱码问题

    在开发或学习当中,我们不可避免的会碰到中文乱码的问题(好想哭,但还是要保持微笑!) 今天,在学习spring boot中碰到了中文乱码问题. 首先,看了一下workspace是不是设置utf-8默认字 ...

  3. spring boot: 中文显示乱码,在applicationContext里面配置

    spring boot: 中文显示乱码,在applicationContext里面配置 applicationContext.properties ########################## ...

  4. spring boot + thymeleaf 乱码问题

    spring boot + thymeleaf 乱码问题 hellotrms 发布于 2017/01/17 15:27 阅读 1K+ 收藏 0 答案 1 开发四年只会写业务代码,分布式高并发都不会还做 ...

  5. spring 解决中文乱码问题

    spring 解决中文乱码问题 使用spring的前提下在web.xml中配置 <filter> <filter-name>encodingFilter</filter- ...

  6. 彻底解决Spring MVC 中文乱码 问题

    1:表单提交controller获得中文参数后乱码解决方案 注意: jsp页面编码设置为UTF-8 form表单提交方式为必须为post,get方式下面spring编码过滤器不起效果 <%@ p ...

  7. spring mvc 中文乱码 post与get的方法解决

    spring mvc表单提交中文参数乱码问题 今天测试spring mvc  ,中文乱码,在web.xml中加上 <filter> <filter-name>encodingF ...

  8. Spring MVC 中文乱码的解决

    对于POST方法提交的中文乱码 , 可在web.xml中添加如下代码 : <filter> <filter-name>encodingFilter</filter-nam ...

  9. spring MVC中文乱码相关总结

    总结几种方式,都使用的话能解决大多数乱码的情况 1.所有页面使用 <%@page language="java" pageEncoding="UTF-8" ...

随机推荐

  1. 用IntelliJ IDEA 配置Maven并部署Maven工程到Tomcat(Windows中)

    近几天做一个新项目才接触Intellij IDEA 1.在官网下载了maven 解压并新建一个本地仓库文件夹 2.配置本地仓库路径 3.配置maven环境变量 4.在IntelliJ IDEA中配置m ...

  2. JSON.stringify()和JSON.parse()的区别

    JSON.stringify()此方法用于将一个对象解析成字符串并返回. JSON.parse()此方法刚好相反是将一个字符串对象解析成一个JSON对象.

  3. STL笔记

    STL的基本概念: 1-容器:是可容纳各种类型的数据结构,是 类模板. 2-迭代器:是用于依次存放容器中的元素,类似指针. 3-算法: 是用于操作容器中元素的 函数模板. sort() 用来对 vec ...

  4. CentOS7.5二进制安装MySQL-5.6.40

    安装依赖 yum install -y gcc gcc-c++ automake autoconf yum -y install cmake bison-devel ncurses-devel lib ...

  5. Pycharm常用的快捷键

    常用快捷键: Ctrl + D              复制选定的区域或行 Ctrl + Y               删除选定的行 Ctrl + Alt + L         代码格式化 Ct ...

  6. springboot的junit4模拟request、response对象

    关键字: MockHttpRequest.Mock测试 问题: 在模拟junit的request.response对象时,会报如下空指针异常. 处理方法: 可用MockHttpServletReque ...

  7. ThinkPHP5.0框架事务处理操作简单示例

    本文介绍ThinkPHP5.0框架事务处理操作,结合实例形式分析了ThinkPHP5针对删除操作的事务处理相关操作技巧,可以加深对ThinkPHP源码的理解,需要的朋友可以参考下 事务的调用在mysq ...

  8. Linux3.5—视屏模块学习与分析

    插入USB摄像头后,我看到了识别出的一些信息,在内核源码中搜到了相关信息: 搜索之后,在uvc_driver.c 帮助文档:linux-3.5/Documentation/video4linux/v4 ...

  9. ACM1000:A + B Problem

    Problem Description Calculate A + B.   Input Each line will contain two integers A and B. Process to ...

  10. 使用bison和yacc制作脚本语言(4)

    我们现在开始设计数据结构: interpreter.h #ifndef INTERPRETER #define INTERPRETER #include "../include/eval.h ...