【问题标题】:Spring Data Redis - Issue while storing DateSpring Data Redis - 存储日期时出现问题
【发布时间】:2019-04-15 11:19:03
【问题描述】:

我正在使用Spring Boot + Spring data Redis 示例将日期保存到 Redis 缓存中。虽然我使用了@DateTimeFormat @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd"),但仍然发生持久性是长期价值。看起来是一毫秒。

如果我需要设置额外的配置来保持日期,例如yyyy-MM-dd,有人可以指导吗?

HGETALL users:1
1) "_class"
2) "com.XXX.entity.User"
3) "userId"
4) "1"
5) "name"
6) "John"
7) "createdDate"
8) "1542043247352"

实体类:

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("users")
public class User {
    @Id
    private Long userId;
    private String name;

    @DateTimeFormat
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    private Date createdDate;
    private List<Group> groups;
}

UPDATE-1:: 根据我实施的建议,但仍然无法正常工作 CustomDateSerializer.java

@Component
public class CustomDateSerializer extends JsonSerializer<Date> {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");

    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        String formattedDate = dateFormat.format(date);
        gen.writeString(formattedDate);
    }
}

自定义界面

@Retention(RetentionPolicy.RUNTIME)
public @interface MyJsonFormat {
    String value();
}

模型类

@MyJsonFormat("dd.MM.yyyy") 
@JsonSerialize(using = CustomDateSerializer.class)
private Date createdDate;

【问题讨论】:

    标签: java spring-boot redis jedis spring-data-redis


    【解决方案1】:

    我建议改用 LocalDateTime(或 LocalDate,如果您愿意)。然后,您可以使用

    注释您的字段
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime createdAt;
    

    使用jackson的jsr310插件:

    import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
    import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
    

    【讨论】:

    • 如果我只想要日期或带时间戳的日期,例如yyyy-MM-dd HH:mm:ss,该怎么办?
    • @PAA 编写自己的序列化器和反序列化器。只需实现与 Jackson 的反序列化器相同的接口
    【解决方案2】:

    通过使用自定义序列化程序,可以解决这个问题。参考@https://kodejava.org/how-to-format-localdate-object-using-jackson/#comment-2027

    public class LocalDateSerializer extends StdSerializer<LocalDate> {
        private static final long serialVersionUID = 1L;
    
        public LocalDateSerializer() {
            super(LocalDate.class);
        }
    
        @Override
        public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException {
            generator.writeString(value.format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
    }
    

    POJO:

    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    private LocalDate createdDate;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2021-09-13
      相关资源
      最近更新 更多