Format date when returning Json Date
Step 1: Create CustomObjectMapper class
/** * Solve the issue that when SpringMVC uses @ResponseBody to return json, the date format is displayed as a timestamp by default. Need to use */ @Component("customObjectMapper") public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper() { CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date.class, new JsonSerializer<Date>() { @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); jsonGenerator.writeString(sdf.format(value)); } }); this.setSerializerFactory(factory); } }Step 2: The configuration is as follows:
<mvc:annotation-driven> <mvc:message-converters> <bean> <property name="objectMapper" ref="customObjectMapper"></property> </bean> </mvc:message-converters> </mvc:annotation-driven>
The effects are as follows:
Before formatting
After formatting
Advanced: Return to the date of custom format
Returns the date format of the json string when using @ResponseBody. The Date type attribute returns a Long type timestamp by default. How can it return a custom date format?
Solution: There are currently two ways to implement it,
1. Partial modifications (more online, but not recommended);
Inherit Jackson's abstract class: JsonSerializer<T>, and then add the annotation @JsonSerialize to the property getter() of the javabean.
The code is as follows:
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; /** * @description Custom return JSON Date formatting processing in data grid*/ public class CustomDateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); jsonGenerator.writeString(sdf.format(value)); } } How to use:
@JsonSerialize(using = CustomDateSerializer.class) public Date getCreateDate() { return createDate; } 2. Global modification (strongly recommended):
MappingJacksonHttpMessageConverter mainly uses ObjectMapper to return json strings. Here we inherit this class and register a JsonSerializer<T>. Then inject a custom ObjectMapper into the configuration file.
The code is as follows:
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.ObjectMapper; import org.codehaus.jackson.map.SerializerProvider; import org.codehaus.jackson.map.ser.CustomSerializerFactory; /** * @description Solve the Date type to return the json format as a custom format*/ public class CustomObjectMapper extends ObjectMapper { public CustomObjectMapper(){ CustomSerializerFactory factory = new CustomSerializerFactory(); factory.addGenericMapping(Date.class, new JsonSerializer<Date>(){ @Override public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); jsonGenerator.writeString(sdf.format(value)); } }); this.setSerializerFactory(factory); } }Configure in spring-servlet.xml:
<mvc:annotation-driven> <mvc:message-converters> <bean> <property name="objectMapper" ref="customObjectMapper"></property> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="customObjectMapper"></bean>