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类型? 抹除掉后面 时间 ? 时间不是查询条件?
随机推荐
- [ES6] 11. String Templates
ECMAscript 6 lets us use string templates to gain a lot more control over strings in JavaScript. var ...
- xUtils怎样通过注解对FindViewById进行封装
之前讲到了介绍了一下xUtils的基本用法,今天我们就来具体介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它採用了一种注解的方式进行声明,那么我们首先 ...
- PortableApps的使用方法
1 从官方网站下载这个软件,建议只下载PortableApps Platform Only即可,因为官方提供的软件其实很少,大多数需要我们自己添加. PortableApps 致力于将一些常见的开源软 ...
- JavaScript 中的string 方法
创建string的方法 var str ="abc"; var str = new String("abc"); var str = String(" ...
- 一个简单的python爬虫(转)
# -*- coding: utf-8 -*- #--------------------------------------- # 程序:百度贴吧爬虫 # 版本:0.1 # 作者:why # 日期: ...
- 理解Linux系统中的load average
理解Linux系统中的load average(图文版) 博客分类: Linux linux load nagios 一.什么是load average? linux系统中的Load对当前CPU工作 ...
- MySQL中 order by 与 limit 的执行顺序以及使用实例
在 MySQL 执行查询的时候,我们可能既要对结果集进行排序又要限制行数,那么此时 order by 与 limit 的执行顺序是怎么样的呢? order by与limit的执行顺序是:先执行orde ...
- 如何为Drupal缓存对象指定缓存类?
什么意思?意思是说,假如你有这样的需求,需要将cache_page缓存到数据库,其它的都缓存到memcache,这该怎么办? 看看_cache_get_object()的实现你就会知道上面的问题该怎么 ...
- Nginx常用配置整理
1.全局块:配置影响nginx全局的指令.一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等. worker_pro ...
- C#设计模式学习笔记-单例模式随笔
最近学习 设计模式,从单例模式入手 啥是单例模式: 要实现一个单例类的话,首先,肯定是不能让用户自行生产的,那就是说明不能让用户new,所以,就必须把构造函数设置成为私有的 因为静态变量的生命周期跟整 ...