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

相关推荐
caimouse1 小时前
Reactos 第 4 章 对象管理 — 4.8 系统调用 NtDuplicateObject / 4.9 系统调用 NtClose
开发语言·windows·架构
夜郎king2 小时前
湖南高考天气查询:基于 HTML5 与百度天气 API 实现页面展示
前端·html5·百度天气实践·天气信息可视化
写代码写到手抽筋8 小时前
5G上行DCI字段判定:端口 流数 PMI选择详解
java·算法·5g
xieliyu.8 小时前
Java算法精讲:双指针(二)
java·开发语言·算法
jeffer_liu9 小时前
Spring AI 生产级实战:裁判员
java·人工智能·后端·spring·大模型
云水一下9 小时前
TypeScript 从零基础到精通(五):高级类型与泛型
前端·javascript·typescript
何以解忧,唯有..9 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
counterxing9 小时前
vibe coding 之后,我更不想打字了
前端·agent·ai编程
雪的季节9 小时前
RabbitMQ详解
开发语言