LocalDate和LocalDateTime返回值序列化

weblog 1357 0 0

没做处理的时候返回类似:"updateTime":"2021-05-06T10:57:27" 这种形况

添加配置:

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class LocalDateTimeSerializerConfig {
    private static final String pattern ="yyyy-MM-dd HH:mm:ss";
    /**
     *  注册响应json格式的LocalDateTime日期格式序列化器,指定格式
     */
    @Bean
    public LocalDateTimeSerializer localDateTimeSerializer() {
        return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    }
    /**
     *  注册json请求方式的LocalDateTime反序列化器,指定格式
     */
    @Bean
    public LocalDateTimeDeserializer localDateTimeDeserializer() {
        return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern));
    }
    /**
     *  格式化日期类型,响应对应格式化日期字符串
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> {
            //返回json格式,前端序列化为字符串
            builder.serializerByType(LocalDateTime.class, localDateTimeSerializer());
            //从json对象日期字符串反序列化为日期对象
            builder.deserializerByType(LocalDateTime.class, localDateTimeDeserializer());
        };
    }
}

加入配置后返回:"updateTime":"2021-05-06 10:57:27"


猜你喜欢
java基础 3540 概念:Java是指把Java对象转换为字节的过程,而Java反是指把字节恢复为Java对象的过程;:对象的最主要的用处就是在传递保存对象的时候,保证对象的完整性
算法基础 1013 LocalDate1.获取当前时间LocalDatetoday=LocalDate.now();2.获取当前月份LocalDatetoday=LocalDate.now();intmonth=today.getMonthValue();3.获取当前日期LocalDatetoday=LocalDate.now();intday=today.getDayOfMonth();4.获取指定日期LocalD
java序列化储存 2781 请参考:http://www.jiajiajia.club/blog/artical/yjw520/161源码下载地址:http://photo.jiajiajia.club
java基础 2713 java网络(socket)中传输对象Person类,注意(两个项目中的Person类字节码要相同)packagesocket;importjava.io.Serializable
算法基础 796 LocalDateTime转Date/***LocalDateTime转Date*@paramlocalDateTime*@return
spring/springmvc 2119 packagecom.dzqc.yx.util;importjava.util.HashMap;/***封装结果
spring/springmvc 2099 springmvc统一异常信息当服务器代码为: @RequestMapping("/res") publicMyAjaxResultres(@RequestParam(name
框架 4887 解决mybatisMap当字段为空时没有属性1.修改mybatis配置文件mybatis:configuration:call-setters-on-nulls:true2.数据库中:3.没有修
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。