解决接收中文的问题
一般通过配置一个Spring提供的Filter即可解决
<!-- 请求参数编码过滤器 -->
<filter>
<filter-name>SetCharacterEncoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
其原理为:由于Filter先于Servlet执行,CharacterEncodingFilter设置Request/Response的CharacterEncoding为配置的UTF-8后,然后把请求继续传递到DispatcherServlet执行。
如果未设置Request里的编码,则Controller获取到的参数则会乱码,因为会采用默认的字符集来解析参数。
输出字符串
如果配置了如下标签:
<mvc:annotation-driven/>
Spring通过MvcNameSpaceHandler添加了很多Parser.
public class MvcNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
registerBeanDefinitionParser("default-servlet-handler", new DefaultServletHandlerBeanDefinitionParser());
registerBeanDefinitionParser("interceptors", new InterceptorsBeanDefinitionParser());
registerBeanDefinitionParser("resources", new ResourcesBeanDefinitionParser());
registerBeanDefinitionParser("view-controller", new ViewControllerBeanDefinitionParser());
registerBeanDefinitionParser("redirect-view-controller", new ViewControllerBeanDefinitionParser());
registerBeanDefinitionParser("status-controller", new ViewControllerBeanDefinitionParser());
registerBeanDefinitionParser("view-resolvers", new ViewResolversBeanDefinitionParser());
registerBeanDefinitionParser("tiles-configurer", new TilesConfigurerBeanDefinitionParser());
registerBeanDefinitionParser("freemarker-configurer", new FreeMarkerConfigurerBeanDefinitionParser());
registerBeanDefinitionParser("velocity-configurer", new VelocityConfigurerBeanDefinitionParser());
registerBeanDefinitionParser("groovy-configurer", new GroovyMarkupConfigurerBeanDefinitionParser());
registerBeanDefinitionParser("script-template-configurer", new ScriptTemplateConfigurerBeanDefinitionParser());
registerBeanDefinitionParser("cors", new CorsBeanDefinitionParser());
}
}
其中AnnotationDrivenBeanDefinitionParser 注册了两个很重要的Bean,用来做查找Controller并做请求调用。
RequestMappingHandlerMapping.class.getName();
RequestMappingHandlerAdapter.class.getName();
同时注册了以下默认的MessageConverter:
ByteArrayHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter SourceHttpMessageConverter AllEncompassingFormHttpMessageConverter
默认的StringHttpMessageConverter的字符集为:ISO-8859-1
因此如果要输出字符串,需要覆盖默认的StringHttpMessageConverter。源码逻辑是:如果配置了message-converters
,则会首先添加,然后再依次添加默认的Converter. 在执行返回值输出时,也是首先遍历自定义的Converter. 如代码:
配置StringHttpMessageConverter有两种方式:
1) 构造方法
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
此时writeAcceptCharset属性为默认的true,表示要输出所有的支持的字符集到Header中,如
这种方式返回了大量的额外文本,增加了传输数据量,所以Spring提供的默认的StringHttpConverter设置的属性为false是有一定的考虑的。
stringConverterDef.getPropertyValues().add(“writeAcceptCharset”, false);
因此自定义的MessageConverter也可以设置该属性为false,则采用如下配置:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="defaultCharset" value="UTF-8"/>
<property name="writeAcceptCharset" value="false"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
输出Json
输出JSON一般有两种方式:
1) Controller方法里,用Json工具库转换为JSON字符串后,再统一输出String,实现见上文。
2) Controller方法里返回一个模型对象,通过框架配置的JsonMessageConverter来转换为Json字符串后输出。
Spring默认的MessageConverter不支持方式二,因此需要使用额外的转换器,一般常用的有FastjsonConverter, Jackson, Gson的转换器。
Fastjson Converter
pom:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.69</version>
</dependency>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
这里覆盖默认的mediatype=”/“,表示当前只支持application/json
如果配置多个,Converter会跟进请求带上来的Accept作裁定。
Jackson Converter
Pom
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="autoDetectFields" value="false"/>
<property name="autoDetectGettersSetters" value="true"/>
<property name="failOnEmptyBeans" value="false"/>
<property name="failOnUnknownProperties" value="false"/>
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
用Jackson输出的Json,Response的content-type被处理为:application/json;charset=utf-8
Content-Type: application/json;charset=utf-8
Date: Sun, 21 Jun 2020 09:21:37 GMT
Server: Jetty(9.4.29.v20200521)
Transfer-Encoding: chunked
Gson Converter
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Response Context-Type 默认也是:application/json
Content-Length: 52
Content-Type: application/json;charset=utf-8
Date: Sun, 21 Jun 2020 09:37:45 GMT
Server: Jetty(9.4.29.v20200521)