目录
问题如下:
解决方案:
1、检查编译软件的编码格式是否正确
eclipse默认的编码格式是GBK,而idea默认编码格式为UTF-8
idea
eclipse
2、检查.xml头文件是否正确
<?xml version="1.0" encoding="UTF-8"?>
3、若使用的SSM框架,可以使用过滤器解决
<!-- 2.配置SpringMVC的乱码过滤 ,所有请求都会被springmvc拦截-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4、在pom.xml加入如下配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
5、或在applicationContext.xml文件中加入乱码配置
<!--JSON乱码问题配置-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="failOnEmptyBeans" value="false"/>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
6、或在pom.xml加入如下即可
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
相关问题:
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 2 字节的 UTF-8 序列的字节 2 无效。
链接: https://blog.csdn.net/weixin_46822367/article/details/115691737?spm=1001.2014.3001.5502.