一般都是使用@DateTimeFormat把传给后台的时间字符串转成Date,使用@JsonFormat把后台传出的Date转成时间字符串,但是@DateTimeFormat只会在类似@RequestParam的请求参数(url拼接的参数才生效,如果是放到RequestBody中的form-data也是无效的)上生效,如果@DateTimeFormat放到@RequestBody下是无效的。

  在@RequestBody中则可以使用@JsonFormat把传给后台的时间字符串转成Date,也就是说@JsonFormat其实既可以把传给后台的时间字符串转成Date也可以把后台传出的Date转成时间字符串

/**
* Controller
**/
@SpringBootApplication
@RestController
public class Application { @RequestMapping("/server")
public Result home(@RequestBody Param param, HttpServletRequest request) {
return new Result().setRtime(param.getTime());
} @RequestMapping("/server1")
public Result home1(Param1 param, HttpServletRequest request) {
return new Result().setRtime(param.getTime());
} public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
} }
/**
* 在@RequestBody中使用@JsonFormat将入参时间字符串转Date
**/
public class Param { @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date time; public Date getTime() {
return time;
} public void setTime(Date time) {
this.time = time;
}
}
/**
* 在类似@RequestParam中将入参时间字符串转Date
**/
public class Param1 { @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date time; public Date getTime() {
return time;
} public void setTime(Date time) {
this.time = time;
}
}
/**
* 使用@JsonFormat将Date转成时间字符串传给前端
**/
public class Result { @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
private Date rtime; public Date getRtime() {
return rtime;
} public Result setRtime(Date time) {
this.rtime = time;
return this;
}
}

@DateTimeFormat无效原因的更多相关文章

  1. spring redis @Cacheable注解使用部分错误及无效原因

    spring redis @Cacheable注解使用部分错误及无效原因 说明:     spring项目用到redis注解无效,解决问题中遇到一堆BUG,各种搜索,看了许多错误解决方案一一测试,对于 ...

  2. Oracle Index 索引无效原因

    索引无效原因 最近遇到一个SQL语句的性能问题,修改功能之前的运行时间平均为0.3s,可是添加新功能后,时间达到了4~5s.虽然几张表的数据量都比较大(都在百万级以上),但是也都有正确创建索引,不知道 ...

  3. 日期格式化时注解@DateTimeFormat无效的问题分析

    作者:汤圆 个人博客:javalover.cc 背景 有时候我们在写接口时,需要把前台传来的日期String类型转为Date类型 这时我们可能会用到@DateTimeFormat注解 在请求数据为非J ...

  4. div层调整zindex属性无效原因分析及解决方法

    在做的过程中,发现了一个很简单却又很多人应该碰到的问题,设置Z-INDEX属性无效.在CSS中,只能通过代码改变层级,这个属性就是z- index,要让z-index起作用有个小小前提,就是元素的po ...

  5. 【转载】div层调整zindex属性无效原因分析及解决方法

    在做的过程中,发现了一个很简单却又很多人应该碰到的问题,设置Z-INDEX属性无效.在CSS中,只能通过代码改变层级,这个属性就是z-index,要让z-index起作用有个小小前提,就是元素的pos ...

  6. 【Web前端】div层调整zindex属性无效原因分析及解决方法

    在做的过程中,发现了一个很简单却又很多人应该碰到的问题,设置Z-INDEX属性无效.在CSS中,只能通过代码改变层级,这个属性就是z- index,要让z-index起作用有个小小前提,就是元素的po ...

  7. [项目总结]论Android Adapter notifyDataSetChanged与notifyDataSetInvalidated无效原因

    最近在开发中遇到一个问题,Adapter中使用notifyDataSetChanged 与notifyDataSetInvalidated无效,经过思考和网上查找,得出如下原因. 首先看一下notif ...

  8. ListView setOnItemClickListener无效原因分析

    前言 最近在做项目的过程中,在使用listview的时候遇到了设置item监听事件的时候在没有回调onItemClick 方法的问题.我的情况是在item中有一个Button按钮.所以不会回调.上百度 ...

  9. Feign使用Hystrix无效原因及解决方法

    最近项目重构使用了Spring Boot和Spring Cloud.这两者结合确实给项目带来了方便,同时也遇到了一些问题.其中使用feign作为服务消费,但是断路器hystrix一直不起作用让人很费解 ...

随机推荐

  1. Kattis - iBoard

    iBoard After years of success with a single-button mouse, a well known computer company has decided ...

  2. 转载:rem的用法

    本文属于转载,为尊重原作者的劳动成果,在此标注原文地址,点击此处浏览. 1.rem(font size of the root element)是指相对于根元素的字体大小的单位,em(font siz ...

  3. 阿里云服务上面部署redis + 本地Redis客户端连接方法

    本文结合自己在阿里云服务器上面搭建redis服务器,在本地redis的客户端Redis Desktop Manager连接成功的操作,将操作中的一些方法做了一些归纳和总结,希望可以帮到有需要的同学. ...

  4. http-server 简介 复制的

    http-server 简介 https://blog.csdn.net/ithanmang/article/details/88375259http-server 是一个简单的零配置的命令行 htt ...

  5. 小程序中 wx.navigateTo 页面跳转没有反应?

    页面js文件中加入 show: function () {wx.navigateTo({url: ‘/pages/show/show’})} 这个函数 目的在于要做跳转到新的页面,但是你可能会遇到一个 ...

  6. centos查看防火墙端口

    #centos7启动防火墙 systemctl start firewalld.service #centos7停止防火墙/关闭防火墙 systemctl stop firewalld.service ...

  7. jQuery练习总结(二)

    --------------------------------------- <!DOCTYPE html> <!DOCTYPE HTML PUBLIC "-//W3C/ ...

  8. 0113针对大数据量SUM的优化-思路

    转自博客:http://bbs.csdn.net/topics/390426801?page=1 优化思路:无论如何你的结果都是要扫描全有表记录,而在456010记录中,的UserName的分布导致这 ...

  9. Advanced Fruits HDU杭电1503【LCS的保存】

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  10. [Tailwind] Extending Tailwind with Responsive Custom Utility Classes

    You are able to extend the custom css with hover, focus, group-hover, responsive variants class in t ...