JSONUtil.toJsonStr time becomes a timestamp

Problem Description

My interface Dateis to receive the date, and then after I get the object parameter, I serialize it into a json string through hutoolit , and then store it in the database. JSONUtil.toJsonStrThen store it in the database and find that this field is a timestamp.

insert image description here

@DateTimeFormat and @JsonFormat The former is to control the format of the param parameter in the request, the latter is to control the date format of the json parameter and the Response response json, the two do not control the date format in the serialization method we manually call!

resolution process

Serialization rules can be specified through JSONConfig!

JSONConfig jsonConfig = new JSONConfig();
jsonConfig.setDateFormat("yyyy-MM-dd");
JSONUtil.toJsonStr(tpDeclareInfoReq, jsonConfig);

In fact, normally, we want to store the json string itself, and we can directly use the String string to receive the date, which can avoid the back and forth conversion between the dates, and I will save whatever is passed by the front end. Taking the date to connect the parameters is mainly related to the judgment of the date in the business code. If there is no judgment, I suggest directly taking the string to connect!

Someone also asked about date serialization on hutool official website: https://gitee.com/dromara/hutool/issues/I47KLR?skip_mobile=true

If there are date types with different rules in the object, there is no way, such as yyyy-MM-dd and then yyyy-MM-dd HH:mm:ss, JSONConfig can only set one. At this time, you need to choose another json serialization tool. After all, hutool is an amateur.

おすすめ

転載: blog.csdn.net/weixin_43888891/article/details/131440384