温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jackson返回JSON时日期少一天

发布时间:2020-06-03 07:22:20 来源:网络 阅读:1337 作者:caoqishun 栏目:软件技术

经测试发现将对象转JSON时,日期类型的返回的值总是比数据库中的值少1
如 数据库的日期为:2017-12-12,转JSON返回时将变成了2017-12-1。解决方法1:
在对象POJO的get方法中使用@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
br/>解决方法1:
在对象POJO的get方法中使用@JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8")
/*timeZone="GMT+8"加上时区 */
@JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
public Date getShenqrq() {
return shenqrq;
}
public void setShenqrq(Date shenqrq) {
this.shenqrq = shenqrq;
}
解决方法2:
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<!-- 处理responseBody 里面日期类型 -->
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
<!-- 时区指定 -->
<property name="timeZone" value="GMT+8" />
<!-- 为null字段时不显示 -->
<property name="serializationInclusion">
<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI