SPRINGMVC Unified settings return to the front-end JSON time format

Source: Internet
Author: User

When we use the @ResponseBody annotations in SPRINGMVC to return JSON data to the front end, we find that time is always a bunch of numbers, and here are two ways to solve time-format problems.

1. Use annotations on the Get method of the Time field

@JsonFormat (pattern = "Yyyy-mm-dd HH:mm:ss", timezone = "gmt+8")

The time that the frontend gets is the time format you set. But this method is a physical activity, you need to add this annotation to each time Type field.

2 to set the time format by configuring the JSON converter in the spring XML configuration file

 @Component ("jacksonobjectmapper " ) The ref-pointing name in the configuration file corresponds to  public  class  Customobjectmapper extends   Objectmapper { private  static  final  long  serialversionuid = 1l; @PostConstruct  public  void  afterpropertiesset () throws   Exception {simpledateformat sdf  = new  SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"  
XML file configuration for spring
<Mvc:annotation-driven>        <mvc:message-converters>                 <Beans:beanclass= "Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">                <Beans:propertyname= "Objectmapper"ref= "jacksonobjectmapper"/>                < Propertyname= "Supportedmediatypes">                    <List>                        <value>Application/json;charset=utf-8</value>                    </List>                </ Property>            </Beans:bean>                 </mvc:message-converters>    </Mvc:annotation-driven>

Springmvc Unified settings return to the front-end JSON time 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.