I have a simple JsonSerializer
in my Spring project:
public class JsonDateTimeSerializer extends JsonSerializer<Date> {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider sp) throws IOException {
gen.writeString(DATE_FORMAT.format(value));
}
}
And use it like:
@JsonSerialize(using = JsonDateTimeSerializer.class)
public Date getDate() {
return date;
}
Do I have to take care of thread-safety and make the DATE_FORMAT
synchronized (as SimpleDateFormat
is not thread-safe)? I am not sure how exactly @JsonSerialize
works - does it create just single serialized instance across all threads? Or does it create separate instance for each transformation?