springMVC返回的json日期为Long的格式化
来源:互联网 发布:网络歌唱大赛 编辑:程序博客网 时间:2024/06/04 18:19
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
- springMVC返回的json日期为Long的格式化
- SpringMVC+MyBatis - 12 spring mvc4返回的json日期为Long的解决方案
- SpringMVC+MyBatis - 12 spring mvc4返回的json日期为Long的解决方案(转)
- SpringMVC+MyBatis - 12 spring mvc4返回的json日期为Long的解决方案
- SpringMVC+MyBatis - 12 spring mvc4返回的json日期为Long的解决方案
- SpringMVC+MyBatis - 12 spring mvc4返回的json日期为Long的解决方案
- spring mvc4返回的json日期为Long的解决方案
- Spring mvc4返回的json日期为Long的解决方案
- spring mvc返回的json日期为Long的解决方案
- 关于json返回日期格式化的解决方案
- 关于json返回日期格式化的解决方案
- 关于json返回日期格式化的解决方案
- 关于JSON返回日期格式化的解决方案
- json返回日期格式化的解决
- springmvc 返回json数据日期格式化方法
- 处理springMVC返回json日期格式化
- springmvc返回json的日期格式问题
- SpringMVC使用@ResponseBody时返回json的日期格式处理为指定格式
- android 隐藏自己的多媒体文件
- Protobuf基于java的官方示例演示
- android 将Json数据动态的添加到String[]数组当中
- 天猫浏览型应用的CDN静态化架构演变
- objc_getClass
- springMVC返回的json日期为Long的格式化
- eclipse关联git的方法
- 2015-10-30-03-Android Log使用心得
- 删除MySQL数据库
- nable to resolve target 'android-19'
- 正数数组的最小不可组成和
- windows下sublime text 2的安装使用
- Maven配置文件settings.xml
- 设置UILable文本的粗体其他字体