【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 天前
【012】图与最短路径:了解即可
java·数据结构
比特森林探险记1 天前
【无标题】
java·前端
椰猫子1 天前
Javaweb(Filter、Listener、AJAX、JSON)
java·开发语言
IT_陈寒1 天前
SpringBoot自动配置把我都整不会了
前端·人工智能·后端
盛世宏博北京1 天前
以太网温湿度传感器运维技巧,提升设备稳定性与使用寿命
开发语言·php·以太网温湿度传感器
朝新_1 天前
【Spring AI 】核心知识体系梳理:从入门到实战
java·人工智能·spring
一 乐1 天前
旅游|基于springboot + vue旅游信息推荐系统(源码+数据库+文档)
java·vue.js·spring boot·论文·旅游·毕设·旅游信息推荐系统
代码改善世界1 天前
【MATLAB初阶】矩阵操作(一)
开发语言·matlab·矩阵
覆东流1 天前
第1天:Python环境搭建 & 第一个程序
开发语言·后端·python
我命由我123451 天前
Android 开发中,关于 Gradle 的 distributionUrl 的一些问题
android·java·java-ee·android studio·android jetpack·android-studio·android runtime