使用 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)
}
}

完美解决了乱码问题

如果觉得我的

作者:一个会写诗的程序员
链接:https://www.jianshu.com/p/718826aee249
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Spring Boot 中文乱码问题解决方案汇总的更多相关文章

  1. spring boot 中文乱码问题

    在刚接触spring boot 2.0的时候,遇到了一些中文乱码的问题,网上找了一些解决方法. 这里自己做个汇总. 在application.properties文件中添加: spring.http. ...

  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 结合Velocity视图出现中文乱码的解决方案

    编码问题一直是个很令人头疼的事,这几天搭了一个Spring MVC+VTL的web框架,发现中文乱码了,这里记录一种解决乱码的方案. 开发环境为eclipse,首先,检查Window->pref ...

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

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

  8. 使用Kettle抽取数据时,出现中文乱码问题解决方案

    使用Kettle在不同的数据库抽取数据时,有时会出现中文乱码问题:其解决方案如下: 1.查看数据库的字符集是否是UTF-8(最常用的字符集) 2.如果数据库设置正确仍然存在中文乱码,则可能是因为有的客 ...

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

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

随机推荐

  1. JQuery官方学习资料(译):$ vs $()

    直到现在,我们一直是通过一个jQuery对象来调用函数的,例如: $( "h1" ).remove();      大多数jQuery函数是通过jQuery对象调用的,这是$.fn ...

  2. 从零开始学安全(十九)●PHP数组函数

    $temp= array(1,2,3,,,,) 创建一个数组赋值给temp $id=range(1,6,2);     成长值   1到6  跨度为2  就是3个长度数组 也可以是字符“a” &quo ...

  3. web站点和windows服务项目发布时如何排除指定文件

    在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...

  4. JDK和Tomcat安装

    JDK安装: 1,选择安装位置,其余默认安装,安装两次,一个是JDK,一个是JRE,安装在两个文件夹中. 2,配置环境变量: 1,新建一个变量,变量名:JAVA_HOME,变量值:C:\Program ...

  5. codevs3002 石子归并 3

    题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子,一次合并的代价为两堆石子的重量和w[i]+w[i+1].问安排怎样的合并顺序,能够使 ...

  6. CentOS7系统搭建外网环境

    理一下思路第一步 Vultr 注册 充值10刀了: 可以支付宝支付.不再需要绑定银行卡 第二步 选择 一个自己中意的款 系统啊流量之类的购买     Deploy New Instance 第三步  ...

  7. 使用Gson将对象类转成Json对象时出现\u003d的问题

    Gson将对象转成Json对象的方法 Gson gson=new Gson(); String json=gson.toJson(Student.class); 这种情况,如果Student属性中的某 ...

  8. 排错-Loadrunner录制打不开浏览器解决方法

    排错-Loadrunner录制打不开浏览器解决方法 by:授客 QQ:1033553122 问题描述: 采用自带的web测试站点http://127.0.0.1:1080/WebTours/,进行录制 ...

  9. Android为TV端助力 SharedPreferences 轻量级存储!

    首先在当前进程也就是当前的项目里面进行存储 SharedPreferences.Editor editor = mContext.getSharedPreferences("tvplay&q ...

  10. (转载)彻底的理解:WebService到底是什么?

    最近老是有人跟我提web service接口,怎么,怎么滴,我觉得很扎耳朵,web service是一种将服务器的服务封装起来的技术,表现为对外提供接口,所以,web service不是一种接口 !! ...