问题:
后端操作后返回时间格式是"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】