SpringMVC中解决POST请求和GET请求中文乱码问题,以及直接访问返回前台的字符串中文乱码问题(json格式)
解决POST请求中文乱码问题
encoding是设置的request乱码问题,forceEncoding是设置的response乱码问题
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--spring5之后就不需要配置forceEncoding了-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
解决GET请求中文乱码问题解决GET请求中文乱码问题
狂老板推荐的大神过滤器
package com.zhang.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* 解决get和post请求,全部乱码的过滤器
*/
public class GenericEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
//处理response的字符编码
HttpServletResponse myResponse = (HttpServletResponse)response;
//转型为与协议相关对象
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
//对request包装增强
HttpServletRequest myRequest = new MyRequest(httpServletRequest);
filterChain.doFilter(myRequest,myResponse);
}
@Override
public void destroy() {
}
}
class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
private boolean hasEncode;
public MyRequest(HttpServletRequest request){
super(request);
this.request = request;
}
@Override
public Map getParameterMap() {
String method = request.getMethod();
if(method.equalsIgnoreCase("post")){
try{
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}else if(method.equalsIgnoreCase("get")){
Map<String,String[]> parameterMap = request.getParameterMap();
if(!hasEncode){//确保get手动编码逻辑只运行一次
for(String parameterName : parameterMap.keySet()){
String[] values = parameterMap.get(parameterName);
if(values != null){
for (int i = 0; i < values.length; i ++){
try{
//处理get乱码
values[i] = new String(values[i].getBytes("ISO-8859-1"), "utf-8");
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
}
}
hasEncode = true;
}
return parameterMap;
}
return super.getParameterMap();
}
@Override
public String getParameter(String name) {
Map<String,String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
if(values == null){
return null;
}
return values[0];
}
@Override
public String[] getParameterValues(String name) {
Map<String,String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
return values;
}
}
怎么配置
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.zhang.filter.GenericEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
JSON乱码问题(两种解决办法)
- 先看现象
- 办法一,@RequestMapping(value = "/j1",produces = "application/json;charset=utf-8"),这样的话,每个方法都得加,很麻烦;
- 办法二,在springmvc-servlet.xml中添加如下配置(需要导入jackson)
<!-- JSON乱码问题配置 -->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<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>
再次访问,也可以解决;