【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】

相关推荐
盐焗鹌鹑蛋1 分钟前
【C++】继承
开发语言·c++
醇氧4 分钟前
Spring 容器 Map 注入机制详解
java·后端·spring
接着奏乐接着舞。29 分钟前
【2026年7月最新】69道RAG面试题
前端·人工智能·后端·aigc·embedding·rag
以和为贵30 分钟前
前端也能搞懂 Agent:从 Function Calling 到自主编排
前端·人工智能·架构
栈溢出了36 分钟前
Java Lambda 表达式笔记
java·开发语言·intellij-idea
会周易的程序员37 分钟前
使用 pybind11 封装 C++ 日志库 microLog 为 Python 模块
java·c++·python·物联网·架构·日志·aiot
zhixingheyi_tian1 小时前
一份GC日志的解读
java·开发语言
czhaii1 小时前
串口1中断收发-C语言-MODBUS协议
c语言·开发语言
风止何安啊1 小时前
🚦 前端并发请求 “交通管制”:别让你的接口堵成早高峰
前端·javascript·面试
J船长1 小时前
扫盲烂笔头:A 记录、CNAME 的作用, Nginx的用途,为啥叫反向代理
前端