springMVC返回的json日期为Long的格式化

springMVC+myBatis查询数据,得到date类型数据,返回json日期为Long数字(毫秒数)的日期格式化方案:
1、springMVC配置文件统一返回格式:

<mvc:annotation-driven>  
    <!-- 处理responseBody 里面日期类型 -->  
        <mvc:message-converters>  
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
                <property name="objectMapper">  
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">  
                        <property name="dateFormat">  
                            <bean class="java.text.SimpleDateFormat">  
                                <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />  
                            </bean>  
                        </property>  
                    </bean>  
                </property>  
            </bean>  
        </mvc:message-converters>  
    </mvc:annotation-driven>

2、jackson 注解处理

@JsonFormat  :
        此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式,比如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date createtime;


@JsonIgnoreProperties : 
         此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。  

@JsonIgnore  :
         此注解用于属性或者方法上(最好是属性上),作用和上面的@JsonIgnoreProperties一样。 

@JsonSerialize  

 // 反序列化一个固定格式的Date    
    @JsonDeserialize(using = CustomDateDeserialize.class)    
    public void setBirthday(Date birthday) {    
        this.birthday = birthday;    
    }   

 // 序列化指定格式的double格式    
    @JsonSerialize(using = CustomDoubleSerialize.class)    
    public double getSalary() {    
        return salary;    
    }    

自定义一个格式化类:
public class CustomDateDeserialize extends JsonDeserializer<Date> {    

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    

    @Override    
    public Date deserialize(JsonParser jp, DeserializationContext ctxt)    
            throws IOException, JsonProcessingException {    

        Date date = null;    
        try {    
            date = sdf.parse(jp.getText());    
        } catch (ParseException e) {    
            e.printStackTrace();    
        }    
        return date;    
    }    
}    

3、自定义注解

import java.io.IOException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  

import com.fasterxml.jackson.core.JsonGenerator;  
import com.fasterxml.jackson.core.JsonProcessingException;  
import com.fasterxml.jackson.databind.JsonSerializer;  
import com.fasterxml.jackson.databind.SerializerProvider;  

/**  
 * ClassName:DateJsonSerializer <br/>  
 * Function: 日期类型格式化,格式化为:yyyy-MM-dd HH:mm:ss 格式. 用法如下:<br/>  
 * Reason:   @JsonSerialize(using=DateJsonSerializer.class) 
 *           @Column(name="BIRTHDAY") 
 *           public Date getBirthday() { 
 *              return birthday; 
 *           } 
 *          . <br/>  
 * Date:     2014年7月10日 下午1:26:08 <br/>  
 * @author   zhangzhaoyu  
 * @version   1.0 
 * @since    JDK 1.7  
 * @see        
 */  
public class DateJsonSerializer extends JsonSerializer<Date> {  

    @Override  
    public void serialize(Date value, JsonGenerator jgen,  
            SerializerProvider provider) throws IOException,  
            JsonProcessingException {  
         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
         String formattedDate = formatter.format(value);  
         jgen.writeString(formattedDate);  
    }  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值