【Java那些事】关于前端收到后端返回的时间格式“2024-04-28T14:48:41“非想要的格式

问题:

后端操作后返回时间格式是"2024-04-28T14:48:41"

而我们想要的是:"2024-04-28 14:48:41",

两个解决方法:

方法一:使用 @JsonFormat注解

java 复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;

    private String username;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" )
    private LocalDateTime createTime;

    @JsonFormat(pattern =  "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;
}

方法二:使用ObjectMapper

java 复制代码
@Configuration
public class WebConfig {
    @Bean
    public ObjectMapper initObjectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }
}

这样属性就不用添加注解啦【强力推荐d=====( ̄▽ ̄*)b】

相关推荐
你听得到111 分钟前
Get 这波之后,我把 Flutter 状态管理重新看了一遍:新项目到底该选谁?
前端·flutter·架构
小红的布丁7 分钟前
BIO、NIO、AIO 与 IO 多路复用:select、poll、epoll 详解
java·数据库·nio
Elastic 中国社区官方博客8 分钟前
在 Elastic 中使用 OpenTelemetry 内容包可视化 OpenTelemetry 数据
大数据·开发语言·数据库·elasticsearch·搜索引擎
lifallen9 分钟前
Flink Checkpoint 流程、Barrier 流动与 RocksDB 排障
java·大数据·flink
C+++Python11 分钟前
如何学习Python的应用领域知识?
开发语言·python·学习
疯狂打码的少年13 分钟前
【Day12 Java转Python】Python工程的“骨架”——模块、包与__name__
java·开发语言·python
希望永不加班18 分钟前
SpringBoot 自定义 Starter:从零开发一个私有 Starter
java·spring boot·后端·spring·mybatis
一天睡25小时19 分钟前
做产品前,先别急着写代码:我是怎么判断一个点子值不值得做的
前端
全栈开发圈22 分钟前
新书速览|MATLAB数据分析与可视化实践:视频教学版
开发语言·matlab·数据分析
霍理迪22 分钟前
TS—函数、类、泛型
前端