Spring Boot-如何为“创建自定义序列化”;java.time.LocalDateTime“;班
我已经用Spring Boot开发了一个rest服务。我希望返回一个json响应,用户的生日为毫秒。 如何将Spring Boot-如何为“创建自定义序列化”;java.time.LocalDateTime“;班,java,json,spring-boot,web,jackson,Java,Json,Spring Boot,Web,Jackson,我已经用Spring Boot开发了一个rest服务。我希望返回一个json响应,用户的生日为毫秒。 如何将java.time.LocalDateTime对象序列化为毫秒 我的模型课: @Entity(name = "users") public class User implements Serializable { @Id @GeneratedValue @Column(name = "user_id") pr
java.time.LocalDateTime
对象序列化为毫秒
我的模型课:
@Entity(name = "users")
public class User implements Serializable {
@Id
@GeneratedValue
@Column(name = "user_id")
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "date_of_birth")
private LocalDateTime dateOfBirth;
. . .
}
目前的答复:
{
. . .
"dateOfBirth":[2018,7,25,7,0],
. . .
}
{
. . .
"dateOfBirth": 1532786354419,
. . .
}
首选响应:
{
. . .
"dateOfBirth":[2018,7,25,7,0],
. . .
}
{
. . .
"dateOfBirth": 1532786354419,
. . .
}
使用@JsonSerialize(使用=CustomSerializer.class)
自定义序列化程序类:
public class CustomSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
//add your custom date parser
gen.writeString(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()+"");
}
}
公共类CustomSerializer扩展JsonSerializer{
@凌驾
public void serialize(LocalDateTime值、JsonGenerator gen、SerializerProvider序列化程序)引发IOException{
//添加自定义日期分析器
gen.writeString(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()+);
}
}
为什么不按以下方式存储日期属性:
@Temporal(TemporalType.TIMESTAMP)
private Date date = new Date();
它以毫秒为单位给出日期。将其格式化为所需输出是应用层的责任。不要对实体本身进行注释。如果不想用
@JsonSerialize
装饰所有对象,可以将对象映射器配置为始终返回long forLocalDateTime
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateSerializer());
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateDeserializer());
objectMapper.registerModule(javaTimeModule);
return objectMapper;
}
以及反序列化和序列化程序
public class LocalDateSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(String.valueOf(value.atZone(ZoneId.systemDefault()).toEpochSecond()));
}
}
public class LocalDateDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(p.getValueAsString())), ZoneId.systemDefault());
}
}
公共类LocalDateSerializer扩展了JsonSerializer{
@凌驾
public void serialize(LocalDateTime值、JsonGenerator gen、SerializerProvider序列化程序)引发IOException{
gen.writeString(String.valueOf(value.atZone(ZoneId.systemDefault()).toepochssecond());
}
}
公共类LocalDateDeserializer扩展JsonDeserializer{
@凌驾
公共LocalDateTime反序列化(JsonParser p,DeserializationContext ctxt)引发IOException{
返回LocalDateTime.ofInstant(Instant.ofepochssecond(Long.parseLong(p.getValueAsString())),ZoneId.systemDefault());
}
}
在您的示例中,您使用的是出生日期,它可能是一个不应该是您首选格式的
LocalDate
。如果存储为毫秒,则还必须知道时区,以便正确地对其进行反序列化,因为给定的毫秒可以解释为许多不同的LocalDateTime。如果它真的是LocalDateTime,则以与时区无关的格式存储它;如果它真的是时间上的一个瞬间,那么就把它表示为时间上的一个瞬间。既然它以毫秒为单位存储它,你就不能用java.util.Date来代替它吗@Jhontonini只回答了自定义序列化程序的使用。现在添加的时间转换我的示例并没有转换为mills,而是转换为Epoch Unix时间戳。