SpringMVC返回Json,自定义Json中Date类型格式
————————————————————————————————————————————————————————————
SpringMVC返回Json,自定义Json中Date类型格式
SpringMVC返回Json数据依赖jackson这个开源的第三方类库。
若不加任何说明情况下Date类型将以时间戳的形式转换为Json并返回。
jackson提供了一些自定义格式的方法。我们只需继承它的抽象类JsonSerializer<T> ,并在指定的属性方法上添加注解@JsonSerialize即可实现。
1.编写Date转换类(yyyy-MM-dd)
public class CustomDateSerializer extends JsonSerializer<Date>
{
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)throws IOException, JsonProcessingException
{
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = format.format(date);
gen.writeString(formattedDate);
}
}
package com.easyui.model;
import java.util.Date;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.format.annotation.DateTimeFormat;
import com.easyui.utils.CustomDateSerializer;
public class User {
private Integer id;
private String username;
private String password;
private String realname;
private String sex;
private Date birthday;
public int getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@JsonSerialize(using=CustomDateSerializer.class)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
SpringMVC返回Json,自定义Json中Date类型格式的更多相关文章
- ORACLE中date类型字段的处理
(1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如'21-8月-2003'或'21-8月 ...
- MySQL中date类型的空值0000-00-00和00:00:00
1.如果mysql中使用了date类型,并且默认值为'0000-00-00', 那么数据库中的'0000-00-00 00:00:00', '0000-00-00', '00:00:00'这三个值是相 ...
- Java中Date类型详解
一.Date类型的初始化 1. Date(int year, int month, int date); 直接写入年份是得不到正确的结果的. 因为java中Date是从1900年开始算的,所以前面的第 ...
- 关于Java读取mysql中date类型字段默认值'0000-00-00'的问题
今天在做项目过程中,查询一个表中数据时总碰到这个问题: java.sql.SQLException:Value '0000-00-00' can not be represented as ...
- Java中Date类型如何向前向后滚动时间,( 附工具类)
Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...
- js中date类型的格式转化为yyyy-MM-dd HH:mm:ss的String类型
在vue中或其他框架中可以在Date的原型链中添加Format的方法,如ruoyi可以写在main.js中更好,如果写在utils还需要去导入包. 正常的js直接放到utils.js就好 Date.p ...
- Java中Date类型与String 类型之间的互相转换
Java中String类型和Date类型之间的转换 我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需 ...
- mybaits中date类型显示时分秒(orcle数据库)
<insert id="insert" parameterType="daSysLoginLog"> insert into DA_SYS_LOGI ...
- 如何查询mysql中date类型的时间范围记录?
java date类型 会不会自动转换 mysql date类型? 抹除掉后面 时间 ? 时间不是查询条件?
随机推荐
- mybatis paramType String Error
我这么写的 <select id="getHotPlayersByDate" parameterType="java.lang.String" resul ...
- android适配的努力
1. 优测 http://utest.qq.com/ 2. 云测 3. 阿里云移动 4. 百度移动测试.
- C# 继承实现父类方法、重写、重载
继承是派生类(子类)去实现(重写<override>.重构<new>)基类(父类)的方法或属性.从而获取在派生类中要实现的功能. 子类调用父类构造方法,在父类中有个实现姓名和年 ...
- kindeditor 图片上传插件
富文本编辑器,kindeditor是比较好用的一款.需要的功能都有,文档.demo也详细.有什么功能去官网看一眼就好. 官网:http://kindeditor.net/ 一些好用的如图片上传,kin ...
- LoadRunner字符串处理 - 补齐字符串
有些时候需要在某个字符串的前面用0补齐,以便满足长度的格式要求. 在LoadRunner中可以封装出一个函数来处理这种问题: /* Function to pad a string to x char ...
- win64 QT(VS2013) 配置 CGAL+libQGLViewer
网上讲的都不是太全 我来说说把~ 首先感谢这位大神: chenwk891:http://blog.csdn.net/chenwk891/article/details/42171495#reply 还 ...
- Docker Container同时启动多服务 supervisor
Docker Container同时启动多服务 转载请注明来自:http://blog.csdn.net/wsscy2004 昨天踩了个天坑,我有一个基本的镜像centos6.5+ssh,是通过Doc ...
- Difference between End-to-end testing and System testing
www.guru99.com/end-to-end-testing.html
- CSS3中伪类nth-child和nth-of-type区别
本篇文章由:http://xinpure.com/css3-pseudo-class-difference-between-nthchild-and-nthoftype/ 首先来看看 nth-chil ...
- bootstrap学习笔记 多媒体对象
本文将介绍Bootstrap中的多媒体对象(Media Object).这些抽象的对象样式用于创建各种类型的组件(比如博客评论),我们可以在组件中使用图文混排,图像可以左对齐或者右对齐.媒体对象可以用 ...