Spring Boot - 如何为“java.time.LocalDateTime”

我用 Spring Boot 开发了一个休息服务。我想以毫秒为单位返回用户生日的 json 响应。如何将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,

. . . 

}


梵蒂冈之花
浏览 131回答 3
3回答

侃侃尔雅

用 @JsonSerialize(using = CustomSerializer.class)@Entity(name = "users")public class User implements Serializable {&nbsp; &nbsp; @Id&nbsp; &nbsp; @GeneratedValue&nbsp; &nbsp; @Column(name = "user_id")&nbsp; &nbsp; private Long id;&nbsp; &nbsp; @Column(name = "first_name")&nbsp; &nbsp; private String firstName;&nbsp; &nbsp; @Column(name = "last_name")&nbsp; &nbsp; private String lastName;&nbsp; &nbsp; @JsonSerialize(using = CustomSerializer.class)&nbsp; &nbsp; @Column(name = "date_of_birth")&nbsp; &nbsp; private LocalDateTime dateOfBirth;&nbsp;&nbsp;&nbsp; &nbsp; . . .}自定义序列化程序类:public class CustomSerializer extends JsonSerializer<LocalDateTime> {@Overridepublic void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {&nbsp; &nbsp; //add your custom date parser&nbsp; &nbsp; gen.writeString(value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()+"");}}

FFIVE

如果您不想装饰所有对象,@JsonSerialize可以将对象映射器配置为始终返回 long for LocalDateTime.@Beanpublic ObjectMapper objectMapper() {&nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; JavaTimeModule javaTimeModule = new JavaTimeModule();&nbsp; &nbsp; javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateSerializer());&nbsp; &nbsp; javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateDeserializer());&nbsp; &nbsp; objectMapper.registerModule(javaTimeModule);&nbsp; &nbsp; return objectMapper;}和 de- 和序列化器。public class LocalDateSerializer extends JsonSerializer<LocalDateTime> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; gen.writeString(String.valueOf(value.atZone(ZoneId.systemDefault()).toEpochSecond()));&nbsp; &nbsp; }}public class LocalDateDeserializer extends JsonDeserializer<LocalDateTime> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; return LocalDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(p.getValueAsString())), ZoneId.systemDefault());&nbsp; &nbsp; }}在您的示例中,您使用的出生日期可能是LocalDate.

隔江千里

为什么不按如下方式存储日期属性:@Temporal(TemporalType.TIMESTAMP)private Date date = new Date();它以毫秒为单位给出日期。将其格式化为所需的输出是应用层的责任。不要在实体本身上做一些注释。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java