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

相关推荐
大时光10 分钟前
js 封装 动画效果
前端
Mr YiRan10 分钟前
C++语言学习之面向对象
java·c++·学习
大时光13 分钟前
html翻页时钟 效果
前端
大猫子的技术日记18 分钟前
2025 AI Agent 开发实战指南:从上下文工程到多智能体协作
前端·人工智能·bootstrap
dc_001221 分钟前
“mysqld --initialize --console ”执行不成功情况总结和解决措施
java
前端达人25 分钟前
被JavaScript忽视的Web Animations API:为什么说它是前端动画的真正未来?
开发语言·前端·javascript·ecmascript
忧郁的橙子.34 分钟前
04-从零搭建本地AI对话系统:Ollama + DeepSeek-R1:7B + Streamlit
前端·chrome
米羊1211 小时前
风险评估文档记录
开发语言·网络·php
摘星编程1 小时前
解锁Agent智能体的未来:五大实战策略彻底革新人机协作模式
java·开发语言