解决接收中文的问题

一般通过配置一个Spring提供的Filter即可解决

  1. <!-- 请求参数编码过滤器 -->
  2. <filter>
  3. <filter-name>SetCharacterEncoding</filter-name>
  4. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  5. <init-param>
  6. <param-name>encoding</param-name>
  7. <param-value>UTF-8</param-value>
  8. </init-param>
  9. <init-param>
  10. <param-name>forceEncoding</param-name>
  11. <param-value>true</param-value>
  12. </init-param>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>SetCharacterEncoding</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>

其原理为:由于Filter先于Servlet执行,CharacterEncodingFilter设置Request/Response的CharacterEncoding为配置的UTF-8后,然后把请求继续传递到DispatcherServlet执行。

如果未设置Request里的编码,则Controller获取到的参数则会乱码,因为会采用默认的字符集来解析参数。

输出字符串

如果配置了如下标签:

  1. <mvc:annotation-driven/>

Spring通过MvcNameSpaceHandler添加了很多Parser.

  1. public class MvcNamespaceHandler extends NamespaceHandlerSupport {
  2. @Override
  3. public void init() {
  4. registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
  5. registerBeanDefinitionParser("default-servlet-handler", new DefaultServletHandlerBeanDefinitionParser());
  6. registerBeanDefinitionParser("interceptors", new InterceptorsBeanDefinitionParser());
  7. registerBeanDefinitionParser("resources", new ResourcesBeanDefinitionParser());
  8. registerBeanDefinitionParser("view-controller", new ViewControllerBeanDefinitionParser());
  9. registerBeanDefinitionParser("redirect-view-controller", new ViewControllerBeanDefinitionParser());
  10. registerBeanDefinitionParser("status-controller", new ViewControllerBeanDefinitionParser());
  11. registerBeanDefinitionParser("view-resolvers", new ViewResolversBeanDefinitionParser());
  12. registerBeanDefinitionParser("tiles-configurer", new TilesConfigurerBeanDefinitionParser());
  13. registerBeanDefinitionParser("freemarker-configurer", new FreeMarkerConfigurerBeanDefinitionParser());
  14. registerBeanDefinitionParser("velocity-configurer", new VelocityConfigurerBeanDefinitionParser());
  15. registerBeanDefinitionParser("groovy-configurer", new GroovyMarkupConfigurerBeanDefinitionParser());
  16. registerBeanDefinitionParser("script-template-configurer", new ScriptTemplateConfigurerBeanDefinitionParser());
  17. registerBeanDefinitionParser("cors", new CorsBeanDefinitionParser());
  18. }
  19. }

其中AnnotationDrivenBeanDefinitionParser 注册了两个很重要的Bean,用来做查找Controller并做请求调用。

RequestMappingHandlerMapping.class.getName();
RequestMappingHandlerAdapter.class.getName();

同时注册了以下默认的MessageConverter:

ByteArrayHttpMessageConverter StringHttpMessageConverter ResourceHttpMessageConverter SourceHttpMessageConverter AllEncompassingFormHttpMessageConverter

默认的StringHttpMessageConverter的字符集为:ISO-8859-1

因此如果要输出字符串,需要覆盖默认的StringHttpMessageConverter。源码逻辑是:如果配置了message-converters,则会首先添加,然后再依次添加默认的Converter. 在执行返回值输出时,也是首先遍历自定义的Converter. 如代码:
image.png
配置StringHttpMessageConverter有两种方式:
1) 构造方法

  1. <mvc:message-converters>
  2. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  3. <constructor-arg value="UTF-8"/>
  4. </bean>
  5. </mvc:message-converters>

此时writeAcceptCharset属性为默认的true,表示要输出所有的支持的字符集到Header中,如

image.png
这种方式返回了大量的额外文本,增加了传输数据量,所以Spring提供的默认的StringHttpConverter设置的属性为false是有一定的考虑的。

stringConverterDef.getPropertyValues().add(“writeAcceptCharset”, false);

因此自定义的MessageConverter也可以设置该属性为false,则采用如下配置:

  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.StringHttpMessageConverter">
  4. <property name="defaultCharset" value="UTF-8"/>
  5. <property name="writeAcceptCharset" value="false"/>
  6. </bean>
  7. </mvc:message-converters>
  8. </mvc:annotation-driven>

输出Json

输出JSON一般有两种方式:
1) Controller方法里,用Json工具库转换为JSON字符串后,再统一输出String,实现见上文。
2) Controller方法里返回一个模型对象,通过框架配置的JsonMessageConverter来转换为Json字符串后输出。

Spring默认的MessageConverter不支持方式二,因此需要使用额外的转换器,一般常用的有FastjsonConverter, Jackson, Gson的转换器。

Fastjson Converter

pom:

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.69</version>
  5. </dependency>
  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
  4. <property name="supportedMediaTypes">
  5. <list>
  6. <value>application/json;charset=UTF-8</value>
  7. </list>
  8. </property>
  9. </bean>
  10. </mvc:message-converters>
  11. </mvc:annotation-driven>

这里覆盖默认的mediatype=”/“,表示当前只支持application/json

如果配置多个,Converter会跟进请求带上来的Accept作裁定。

Jackson Converter

Pom

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-core</artifactId>
  4. <version>2.9.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-annotations</artifactId>
  9. <version>2.9.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.fasterxml.jackson.core</groupId>
  13. <artifactId>jackson-databind</artifactId>
  14. <version>2.9.3</version>
  15. </dependency>
  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
  4. <property name="objectMapper">
  5. <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
  6. <property name="autoDetectFields" value="false"/>
  7. <property name="autoDetectGettersSetters" value="true"/>
  8. <property name="failOnEmptyBeans" value="false"/>
  9. <property name="failOnUnknownProperties" value="false"/>
  10. <property name="dateFormat">
  11. <bean class="java.text.SimpleDateFormat">
  12. <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss"/>
  13. </bean>
  14. </property>
  15. </bean>
  16. </property>
  17. </bean>
  18. </mvc:message-converters>
  19. </mvc:annotation-driven>

用Jackson输出的Json,Response的content-type被处理为:application/json;charset=utf-8

  1. Content-Type: application/json;charset=utf-8
  2. Date: Sun, 21 Jun 2020 09:21:37 GMT
  3. Server: Jetty(9.4.29.v20200521)
  4. Transfer-Encoding: chunked

Gson Converter

  1. <dependency>
  2. <groupId>com.google.code.gson</groupId>
  3. <artifactId>gson</artifactId>
  4. <version>2.3.1</version>
  5. </dependency>
  1. <mvc:annotation-driven>
  2. <mvc:message-converters>
  3. <bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
  4. </mvc:message-converters>
  5. </mvc:annotation-driven>

Response Context-Type 默认也是:application/json

  1. Content-Length: 52
  2. Content-Type: application/json;charset=utf-8
  3. Date: Sun, 21 Jun 2020 09:37:45 GMT
  4. Server: Jetty(9.4.29.v20200521)