The solution to the problem that SpringMVC returns the date format of json

The date type output of josn in springMVC is a timestamp by default, and date format conversion is required.

formatted output json

 method 1,

  Add @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") to the getter method of the entity class

  You can format the json date. Related jar packages of json that need to be imported  

  @JsonFromat exists in jackson-annotations-2.1.0.jar.
  Dependencies that Maven projects need to add:    

<!-- json数据 -->
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>${jackson.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>${jackson.core.version}</version>
</dependency>

<properties>
    <jackson.version>1.9.13</jackson.version>
    <jackson.core.version>2.4.2</jackson.core.version>
</properties>

View maven's jar package dependencies in IDEA  

 

  Advantages and disadvantages:

    The advantage is that it is simple and convenient, and the disadvantage is that it needs to be added to the getter method of each required property. It is cumbersome to look at macroscopically, but in actual development

    It's just a line of code, the only bad thing is that mybatis automatically generates entity classes that will be overwritten.

 Method 2,  

  Although the above method is simple and convenient, the disadvantages are also obvious. The automatically generated code will overwrite the entity class, and each date attribute must be added manually.

  In practice, the date attribute is a common necessity. Therefore, it can be processed globally and in a unified format. It needs to be said here that the date and timestamp in the database

  will be converted to date objects by mybatis. As for the format specification that the birthday is accurate to the day and the time is accurate to the second, the display layer can handle it.

  Unified into yyyy-MM-dd HH:mm:ss.

  MappingJacksonHttpMessageConverter mainly returns json string through ObjectMapper. Here we inherit this class and register

  A JsonSerializer<T>. Then inject a custom ObjectMapper in the configuration file.

  Step 1: Write a subclass to inherit ObjectMapper  

package cn.ll.ssm.controller.converter;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.codehaus.jackson.JsonGenerator;
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;

/**      
* Project name: springmvc_02_ssm   
* Class name: CustomJsonDateConverter   
* Class description: Solve the Date type and return the json format to a custom format
* Created by: LL   
* Created: April 29, 2018 11:13:08 AM      
* @version       
*/
public class CustomJsonDateConverter extends ObjectMapper {
    public CustomJsonDateConverter(){
        CustomSerializerFactory factory = new CustomSerializerFactory();
        factory.addGenericMapping(Date.class, new JsonSerializer<Date>(){
          @Override
          public void serialize(Date value,
                     JsonGenerator jsonGenerator,
                     SerializerProvider provider)
              throws IOException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonGenerator.writeString(sdf.format(value));
          }
        });
        this.setSerializerFactory(factory);
      }
}

Step 2: Configure in SpringMVC.xml  

<mvc:annotation-driven conversion-service="conversionService" validator="validator">
         <mvc:message-converters>
          <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="objectMapper" ref="customObjectMapper"></property>
          </bean>
        </mvc:message-converters>
     </mvc:annotation-driven>
    
    <bean id="customObjectMapper" class="cn.ll.ssm.controller.converter.CustomJsonDateConverter"></bean>

third step:

  This configuration cannot be used with the previous @JsonFormat. Since the date format is globally unified, date and datetime and timestamp are both

A format, if the date field such as birthday needs to be simplified, it can only be cropped in the display layer.

 

Reference: http://www.jb51.net/article/103032.htm

  

   

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325041433&siteId=291194637