[Turn] SpringMVC returns the date format of json when using @ResponseBody, pay attention to the use of @DatetimeFormat

1. SpringMVC returns the date format of json when using @ResponseBody

 

     Prerequisites: @ResponseBody The core class that returns json strings is org.springframework.http.converter.json.MappingJacksonHttpMessageConverter, which uses Jackson, an open source third-party library. Mainly the following two jar packages: jackson-core-asl-1.6.4.jar; jackson-mapper-asl-1.6.4.jar.

    Problem arises: Date format returned as json string when using @ResponseBody. The Date type attribute returns a Long timestamp by default. How can I return a custom date format?

    Solution: There are currently two ways to achieve,

             1. Partial modification (there are many online, but not recommended);

          Inherit Jackson's abstract class: JsonSerializer<T>, and then add the annotation @JsonSerialize to the property getter() of the javabean to achieve this.

         code show as below:

 

Java code   Favorite code
  1. import java.io.IOException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4.   
  5. import org.codehaus.jackson.JsonGenerator;  
  6. import org.codehaus.jackson.JsonProcessingException;  
  7. import org.codehaus.jackson.map.JsonSerializer;  
  8. import org.codehaus.jackson.map.SerializerProvider;  
  9.   
  10. /** 
  11.  * @description Customize the date formatting processing in the returned JSON data grid 
  12.  * @author aokunsang 
  13.  * @date 2013-5-28 
  14.  */  
  15. public class CustomDateSerializer extends JsonSerializer<Date> {  
  16.   
  17.     @Override  
  18.     public void serialize(Date value,   
  19.             JsonGenerator jsonGenerator,   
  20.             SerializerProvider provider)  
  21.             throws IOException, JsonProcessingException {  
  22.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  23.         jsonGenerator.writeString(sdf.format(value));  
  24.     }  
  25. }  

    How to use:

    

Java code   Favorite code
  1. @JsonSerialize(using = CustomDateSerializer.class)  
  2. public Date getCreateDate() {  
  3.     return createDate;  
  4. }  

             2、全局修改(强烈推荐):

         MappingJacksonHttpMessageConverter主要通过ObjectMapper来实现返回json字符串。这里我们继承该类,注册一个JsonSerializer<T>。然后在配置文件中注入自定义的ObjectMapper。

        代码如下:

Java代码   Favorite code
  1. import java.io.IOException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4.   
  5. import org.codehaus.jackson.JsonGenerator;  
  6. import org.codehaus.jackson.JsonProcessingException;  
  7. import org.codehaus.jackson.map.JsonSerializer;  
  8. import org.codehaus.jackson.map.ObjectMapper;  
  9. import org.codehaus.jackson.map.SerializerProvider;  
  10. import org.codehaus.jackson.map.ser.CustomSerializerFactory;  
  11.   
  12. /** 
  13.  * @description 解决Date类型返回json格式为自定义格式 
  14.  * @author aokunsang 
  15.  * @date 2013-5-28 
  16.  */  

  17. public class CustomObjectMapper extends ObjectMapper {  
  18.   
  19.     public CustomObjectMapper(){  
  20.         CustomSerializerFactory factory = new CustomSerializerFactory();  
  21.         factory.addGenericMapping(Date.classnew JsonSerializer<Date>(){  
  22.             @Override  
  23.             public void serialize(Date value,   
  24.                     JsonGenerator jsonGenerator,   
  25.                     SerializerProvider provider)  
  26.                     throws IOException, JsonProcessingException {  
  27.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  28.                 jsonGenerator.writeString(sdf.format(value));  
  29.             }  
  30.         });  
  31.         this.setSerializerFactory(factory);  
  32.     }  
  33. }  

   spring-servlet.xml中配置:

Java代码   Favorite code
  1. <mvc:annotation-driven>  
  2.         <mvc:message-converters>  
  3.             <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  4.                 <property name="objectMapper" ref="customObjectMapper"></property>  
  5.             </bean>  
  6.         </mvc:message-converters>  
  7.     </mvc:annotation-driven>  
  8.    <bean id="customObjectMapper" class="com.pmc.dwa.common.custom.CustomObjectMapper"></bean> 

 Second, the use of @DatetimeFormat attention

     1. It is very simple to use @DatetimeFormat. It should be noted here that a class library joda-time-1.3.jar should be introduced when using it, otherwise the corresponding path will not be accessible (400 error). 

ps: This annotation can be used at the METHOD, FIELD and PARAMETER levels.

Use introduction reference: http://www.captaindebug.com/2011/08/using-spring-3-datetimeformat.html#.UaR3mWWZk0k

   2. Because springMVC does not provide a default date converter, how to convert the date string passed from the previous page to a date type? If there is no global date converter or data binding, you can use the @DatetimeFormat annotation to complete.

Guess you like

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