【发布时间】: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