com.foreveross.springboot.dubbo.autoconfigure.JsonDateSerializer Maven / Gradle / Ivy
package com.foreveross.springboot.dubbo.autoconfigure;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by swChen on 2016/5/13.
* 日期处理类
* 适用于domain对象注解,表示序列化的时间格式
*/
@Component
public class JsonDateSerializer extends JsonSerializer {
@Value("${dubbox.date.format:yyyy-MM-dd HH:mm:ss}")
private static String DATE_DEFAULT_PATTERN;
private static final SimpleDateFormat dateFormat;
static {
DATE_DEFAULT_PATTERN= StringUtils.isEmpty(DATE_DEFAULT_PATTERN)?"yyyy-MM-dd HH:mm:ss":DATE_DEFAULT_PATTERN;
dateFormat = new SimpleDateFormat(DATE_DEFAULT_PATTERN);
}
@Override
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {
String formattedDate = dateFormat.format(date);
gen.writeString(formattedDate);
}
}