依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-avro</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
pojo对象
@Data
public class TestEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 修改时间
*/
private LocalDateTime updateTime;
}
全局配置
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class GlobalConfig {
@Bean
public ObjectMapper initObjectMapper(){
ObjectMapper objectMapper=new ObjectMapper();
JavaTimeModule javaTimeModule=new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//localDateTime按照 "yyyy-MM-dd HH:mm:ss"的格式进行序列化、反序列化
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
}
测试
1、数据准备
image.png
2、加全局配置测试返回数据
{
"status":0,
"data":[
{
"createTime":"2021-04-12 11:37:43",
"updateTime":"2021-04-12 11:40:01"
},
{
"createTime":"2021-04-12 11:41:18",
"updateTime":"2021-04-12 11:41:42"
},
{
"createTime":"2021-04-12 11:43:10",
"updateTime":"2021-04-12 11:43:20"
},
{
"createTime":null,
"updateTime":null
}
]
}
3、不加配置测试返回数据
{
"status":0,
"data":[
{
"createTime":"2021-04-12T11:37:43",
"updateTime":"2021-04-12T11:40:01"
},
{
"createTime":"2021-04-12T11:41:18",
"updateTime":"2021-04-12T11:41:42"
},
{
"createTime":"2021-04-12T11:43:10",
"updateTime":"2021-04-12T11:43:20"
},
{
"createTime":null,
"updateTime":null
}
]
}
参考链接:https://blog.csdn.net/weixin_43931625/article/details/107395262
网友评论