springmvc解决前端到后端日期乱码问题

解决前端到后端日期乱码问题

1、解决前端传至后端乱码问题

spring配置文件中增加

1
2
3
4
5
6
7
8

<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.james.ssm.controller.converter.CustomDataConverter"></bean>
</list>
</property>
</bean>
1
2
3
4
5
6
7
8
9
10
11
12
13
public class   implements Converter<String,Date>{

public Date convert(String source) {
//"yyyy-MM-dd HH:mm:ss"
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
return simpleDateFormat.parse(source);
}catch (ParseException e){
e.printStackTrace();
}
return null;
}
}

3、同时在pojo类中getter方法加如下注解(解决后端传至前端日期格式是长整形数字的问题)

1
2
3
4
@JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8")
public Date getJxkssj() {
return jxkssj;
}