您的位置:首页 > 编程语言 > Java开发

Spring MVC--JSON数据传递中文乱码、日期格式显示错误问题

2019-03-01 19:10 1161 查看

控制器的处理方法使用@ResponseBody注解向前台页面以JSON格式进行数据传递的时候,若返回值是中文字符串,则会出现中文乱码,原因是消息转换器中StringHttpMessageConverter中固定了转换字符编码,则“ISO-8859-1”。

一、JSON数据传递中文乱码问题

常用的两种解决方法

1、在控制器处理方法上的@RequestMapping注解中配置produces

@RequestMapping(value="/view",method=RequestMethod.GET,
produces={"application/json;charset=UTF-8"})
@ResponseBody
public Object view(@RequestParam String id){
//省略处理请求内容
return obj;
}

注:

  • produces:指定返回的内容类型。

  • produces={"application/json;charset=UTF-8"}

    表示该处理方法将产生JSON格式的数据,此时会根据请求报文头中的Accept进行匹配,若请求报文头
    "Accept:application/json"
    时即可匹配,并且字符串的转换编码为"UTF-8"。

2、一次性永久配置:装配消息转换器StringHttpMessageConverter,设置字符编码为UTF-8,修改配置文件Spring MVC配置文件springmvc-servlet.xml,如下

<!-- 装配消息转换器StringHttpMessageConverter,解决中文乱码 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

注: 在上面配置中,通过设置 StringHttpMessageConverter 中的supportedMediaTypes 属性指定媒体类型为

application/json
,字符编码为"UTF-8"。

二、JSON数据传递的日期格式问题

Spring MVC中使用@ResponseBody返回JSON数据时,日期格式默认显示为时间戳(如:517939003300),所以需要转换时间为可读格式如:yyyy-MM-dd。

此处有两种解决方法:
注:以下两种解决方式需要导入JSON包 fastjson-1.2.13.jar

1、注解方式:@JSONField(format=“yyyy-MM-dd”)

直接给需要转换的日期类型属性标注该注解

例:

package cn.smbms.pojo;

import java.util.Date;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import com.alibaba.fastjson.annotation.JSONField;

/**
* 用户实体类
* @author 14062
*
*/
public class User implements java.io.Serializable{

private Integer id;	//主键id
private String userPassword;	//用户密码
private Integer gender;	//性别

@JSONField(format="yyyy-MM-dd")
private Date birthday;	//出生日期

//省略getter/setter方法
}

注:

  • format参数指定转换后的日期格式
  • 此种方式简单直接,但是存在一定的缺点:代码具有强侵入性,紧耦合,并且修改麻烦,所以一般不采用这种硬编码的方式来处理。

2、配置FastJson的消息转换器-----FastJsonHttpMessageConverter

在Spring MVC中,需要进行JSON转换时,通常会使用FastJson提供的FastJsonHttpMessageConverter 来完成。使用FastJsonHttpMessageConverter 的序列化属性WriteDateUseDateFormat 配置使用默认日期类型。

修改SpringMVC 配置文件springmvc-servlet.xml

<mvc:annotation-driven>
<mvc:message-converters>
<!--配置FastJsonHttpMessageConverter消息转换器,解决日期格式问题  -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
<value>application/json</value>
</list>
</property>
<property name="features">
<list>
<!--输出Date的日期转换器  -->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

小结:

  • 若没有配置消息转换器的

    <value>WriteDateUseDateFormat</value>
    ,并且也没有加入属性注解
    @JSONField(format="yyyy-MM-dd")
    ,则转换为会输出时间戳。

  • 若只配置了

    <value>WriteDateUseDateFormat</value>
    ,则会转换输出 yyyy-MM-dd HH:mm:ss 格式的日期。

  • 若只配置了

    <value>WriteDateUseDateFormat</value>

    则会转换输出yyyy-MM-dd HH:mm:ss格式的日期。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐