5月17日 日期格式遇到一些问题

1. @DateTimeFormat 和 @JsonFormat 注解

参考链接:https://blog.csdn.net/zhou520yue520/article/details/81348926

总结:

  • 注解@JsonFormat主要是后台到前台的时间格式的转换【入参】

  • 注解@DataFormAT主要是前后到后台的时间格式的转换【出参】

    jackson在序列化时间时是按照国际标准时间GMT进行格式化的,而在国内默认时区使用的是CST时区,两者相差8小时。

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date symstarttime;

2. post的请求,默认请求头contentType为:application/x-www-form-urlencoded; charset=UTF-8

如果传递的json数据,请求头需要加上 contentType:'application/json',
例如ajax:

var info ={
    username:'admin',
    password:'123123'
};
$.ajax({
    type: 'post',
    url:'/contentType',
    contentType:'application/json',
    data: JSON.stringify(info),
    dataType:'json',
    success:function (data) {

    }
})

另外,post用application/json:长度几十万,后端都可以接收;
但是get方法表单传值和json传值是有限制的,太多服务端会报错:

3.前端js,json的互相转换

  JSON字符串转JSON对象 JSON.parse(jsonstr);
  JS对象(JSON对象)转JSON字符串JSON.stringify(jsonobj);

4. 前后端JOSN数据传递

  前端请求传Json对象则后端使用@RequestParam;

  前端请求传Json对象的字符串则后端使用@RequestBody。

参考链接:https://juejin.im/post/5b5efff0e51d45198469acea
https://blog.csdn.net/feiyst/article/details/88431621

5.Date 和 LocaDateTime

https://blog.csdn.net/a13794479495/article/details/83892829

posted @ 2020-05-17 17:39  proper128  阅读(176)  评论(0编辑  收藏  举报