• 使用mybatis-plus的时候出现了LocalDateTime类(jdk8 中新出现的类
  • 那么我在反序列化的时候出了问题。

我在springboot 2.1.3 中使用以下类结局问题)

  • 用到了下面这个类解决了所有的问题。包括前端传值过来也能直接接受字符串
  • 百度上面也猜了许许多多的坑 最终换了一个spring boot的版本 然后用以下的代码配置完美解决了问题


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter; import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale; /**
* 接收时间配置bean
*/
@Configuration
public class LocalDateTimeFormatConfig { @Bean(name = "mapperObject")
public ObjectMapper getObjectMapper() {
ObjectMapper om = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
om.registerModule(javaTimeModule);
return om;
} @Bean
public Formatter<LocalDate> localDateFormatter() {
return new Formatter<LocalDate>() {
@Override
public String print(LocalDate object, Locale locale) {
return object.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} @Override
public LocalDate parse(String text, Locale locale) throws ParseException {
return LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
};
} @Bean
public Formatter<LocalDateTime> localDateTimeFormatter() {
return new Formatter<LocalDateTime>() {
@Override
public String print(LocalDateTime object, Locale locale) {
return object.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} @Override
public LocalDateTime parse(String text, Locale locale) throws ParseException {
return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
};
} @Bean
public Formatter<LocalTime> localTimeFormatter() {
return new Formatter<LocalTime>() {
@Override
public String print(LocalTime object, Locale locale) {
return object.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
} @Override
public LocalTime parse(String text, Locale locale) throws ParseException {
return LocalTime.parse(text, DateTimeFormatter.ofPattern("HH:mm:ss"));
}
};
} }

LocalDateTime反序列化,LocalDateTime格式化的更多相关文章

  1. 解决Jackson2反序列化LocalDateTime报错

    今天在整合redis和spring boot的时候,遇到了一个错误,记录一下. 报错如下: Could not read JSON: Cannot construct instance of `jav ...

  2. Java基础进阶:时间类要点摘要,时间Date类实现格式化与解析源码实现详解,LocalDateTime时间类格式化与解析源码实现详解,Period,Duration获取时间间隔与源码实现,程序异常解析与处理方式

    要点摘要 课堂笔记 日期相关 JDK7 日期类-Date 概述 表示一个时间点对象,这个时间点是以1970年1月1日为参考点; 作用 可以通过该类的对象,表示一个时间,并面向对象操作时间; 构造方法 ...

  3. fastjson反序列化LocalDateTime失败的问题java.time.format.DateTimeParseException: Text '2019-05-24 13:52:11' could not be parsed at index 10

    本地java类 import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; ...

  4. Java日期时间API系列13-----Jdk8中java.time包中的新的日期时间API类,时间类转换,Date转LocalDateTime,LocalDateTime转Date等

    从前面的系列博客中可以看出Jdk8中java.time包中的新的日期时间API类设计的很好,但Date由于使用仍非常广泛,这就涉及到Date转LocalDateTime,LocalDateTime转D ...

  5. springboot Thymeleaf中格式化jsr310新日期时间类(LocalDateTime,LocalDate)--thymeleaf格式化LocalDateTime,LocalDate等JDK8新时间类

    依赖maven包 <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>th ...

  6. springmvc fastjson 反序列化时间格式化

    第一种情况是从后台拿到数据,进行反序列化,反序列化格式时间:试了一下很多网上的方法,最后发现还是在实体类上面的日期字段加上如下注解,可以完成格式化操作,否则默认就都是时间戳的格式: @JSONFiel ...

  7. Java日期时间API系列19-----Jdk8中java.time包中的新的日期时间API类,ZonedDateTime与ZoneId和LocalDateTime的关系,ZonedDateTime格式化和时区转换等。

    通过Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类中时间范围示意图:可以很清晰的看出ZonedDateTime相当于LocalDateTime+ZoneI ...

  8. 为啥你用@JsonFormat注解时,LocalDateTime会反序列化失败?

    写在前面 最近,有个小伙伴问我:我在SpringBoot项目中,使用@JsonFormat注解标注LocalDateTime类型的字段时,LocalDateTime反序列化失败,这个我该怎么处理呢?别 ...

  9. java 常用类库:格式化NumberFormat;SimpleDataFormat类(转换Data()对象);DateTimeFormatter 转换LocalDateTime时间对象

    NumberFormat类 该类是一个做数字格式化的类,它是一个抽象类,无法实例化.它提供了parse()和format()方法,其中format用于将数值,格式转化成字符串,parse()用于把字符 ...

随机推荐

  1. STM8L052低功耗模式

    Stm8L系列单片机的低功耗有五种模式: § wait模式 § Lowpower run模式 § Lowpower wait模式 § Active-haltwith full RTC模式 § Halt ...

  2. 畅通工程续 -- HDU 1874 floyd

    题目大意: 现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. 思路: floyd算法模板题,这是一个牺牲空间换取时间的算法,本质是动态规划. AC代码: #include < ...

  3. PBRT笔记(11)——光源

    自发光灯光 至今为止,人们发明了很多光源,现在被广泛使用的有: 白炽灯的钨丝很小.电流通过灯丝时,使得灯丝升温,从而使灯丝发出电磁波,其波长的分布取决于灯丝的温度.但大部分能量都被转化为热能而不是光能 ...

  4. [Linux] 使用Yum在CentOS上安装MySQL

    跟随官网上的安装教程:https://dev.mysql.com/doc/refman/8.0/en/linux-installation-yum-repo.html官网上还有一个QuickGuide ...

  5. 基础java中的package的命名规则和import的使用

    包的命名一般用公司域名但是注意域名后辍要放前面如下 package com.cnblogs.i.Cat//对应地址是com/cnblos/i/cat.class也就是Cat.class的地址 如果想将 ...

  6. input标签实现小数点后两位保留小数

    短短一行代码就可以实现 <input type="number" min="0" max="100" step="0.01& ...

  7. string 转 int

    1.stringstream 用流转换 cin    cout都是流的操作   iostream cin的时候,从屏幕读取字符串流,自动判断类型(省去了scanf的格式控制) stringstream ...

  8. idea设置调用方法时提示方法注释

    如图所示:打开file-->setting-->Editor-->General,搜索show,然后勾选上Show quick documentation on mouse move ...

  9. linux系统做raid

    raid 常用步骤 1.ctrl+R 进入raid设置界面 2.F2 相当于右键功能 3.箭头 → 是下一个选项功能 4.ctrl+n是下一页,ctrl+p是前一页 5.Esc退出.最后ctrl+al ...

  10. jsp 表单回显

    1.在表单各个字段中添加value属性,如:value="${user.reloginpass }" 2.在表单提交对应的servlet中封装数据到uer中,如:req.setAt ...