By default, Jackson converts the JSON date
The milliseconds of time are completely out of sight, to show 2015-08-25 18:20:55 There are two ways to modify this format:
1. Format this JSON data with JS code
2. Handling at Jackson conversion
The second method is obviously more convenient. Two steps to complete
First, modify the configuration in the spring configuration file
<!--Register the JSON converter and customize the date format conversion - <Mvc:annotation-driven> <mvc:message-converters> <Beanclass= "Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> < Propertyname= "Objectmapper"> <Beanclass= "Com.ywlaker.tools.ToolJacksonDateFormat"></Bean> </ Property> </Bean> </mvc:message-converters> </Mvc:annotation-driven>
View Code
Then implement the Com.ywlaker.tools.ToolJacksonDateFormat class
PackageCom.ywlaker.tools;Importjava.io.IOException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.codehaus.jackson.JsonGenerator;Importorg.codehaus.jackson.JsonProcessingException;ImportOrg.codehaus.jackson.map.JsonSerializer;ImportOrg.codehaus.jackson.map.ObjectMapper;ImportOrg.codehaus.jackson.map.SerializerProvider;Importorg.codehaus.jackson.map.ser.CustomSerializerFactory;/*** @ Function Description: Jackson to JSON when formatted date display, global settings *@authoryang.wei* @time August 18, 2015 afternoon 2:43:14*@version0.0.1*/ Public classTooljacksondateformatextendsObjectmapper { PublicTooljacksondateformat () {Customserializerfactory factory=Newcustomserializerfactory (); Factory.addgenericmapping (Date.class,NewJsonserializer<date>() {@Override Public voidSerialize (date date, Jsongenerator jsongenerator, Serializerprovider arg2)throwsIOException, jsonprocessingexception {simpledateformat SDF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Jsongenerator.writestring (Sdf.format (date)); }}); This. Setserializerfactory (Factory); }}
View Code
All dates will be processed after they are converted to JSON.
SPRINGMVC's Jackson date format