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

相关推荐
漂流瓶jz2 小时前
总结CSS组件化演进之路:命名规范/CSS Modules/CSS in JS/原子化CSS
前端·javascript·css
踩着两条虫2 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB2 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
Jagger_3 小时前
项目上线忙碌结束之后,为什么总想找点事做?
前端
budingxiaomoli3 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫3 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁3 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
GalenZhang8883 小时前
OpenClaw 配置多个飞书账号实战指南
前端·chrome·飞书·openclaw
即使再小的船也能远航3 小时前
【Python】安装
开发语言·python
学习中.........3 小时前
从扰动函数的变化,感受红黑树带来的性能提升
java