SPRINGMVC's Jackson date format

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.