Recently, when using SpringMvc to make the Http interface, the other party found that the default conversion of the Date format to long when calling my interface, so what I saw on the front-end page was a string of numbers.
We can customize the code converter, and when returning the data to the foreground, we can return the formatted string type data as we need.
package com.cnpc.mall.web.utils; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.map.JsonSerializer; import org.codehaus.jackson.map.SerializerProvider; public class CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value)); } }Note that with the converter, don't forget to add annotations to the domain field and call the converter we defined when converting json:
@JsonSerialize(using = CustomDateSerializer.class) public Date getLsd06() { return lsd06; }Here I add it to the get method, and when returning to the front end, the converter we defined will be called.
The above is the full content of SpringMVC's JsonSerialize date conversion method brought to you by the editor. I hope everyone will support Wulin.com more~