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:
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.codehaus.jackson.JsonGenerator;
- import org.codehaus.jackson.JsonProcessingException;
- import org.codehaus.jackson.map.JsonSerializer;
- import org.codehaus.jackson.map.SerializerProvider;
- /**
- * @description Customize the date formatting processing in the returned JSON data grid
- * @author aokunsang
- * @date 2013-5-28
- */
- public class CustomDateSerializer extends JsonSerializer<Date> {
- @Override
- public void serialize(Date value,
- JsonGenerator jsonGenerator,
- SerializerProvider provider)
- throws IOException, JsonProcessingException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- jsonGenerator.writeString(sdf.format(value));
- }
- }
How to use:
- @JsonSerialize(using = CustomDateSerializer.class)
- public Date getCreateDate() {
- return createDate;
- }
2、全局修改(强烈推荐):
MappingJacksonHttpMessageConverter主要通过ObjectMapper来实现返回json字符串。这里我们继承该类,注册一个JsonSerializer<T>。然后在配置文件中注入自定义的ObjectMapper。
代码如下:
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.codehaus.jackson.JsonGenerator;
- import org.codehaus.jackson.JsonProcessingException;
- 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;
- /**
- * @description 解决Date类型返回json格式为自定义格式
- * @author aokunsang
- * @date 2013-5-28
- */
- public class CustomObjectMapper extends ObjectMapper {
- public CustomObjectMapper(){
- CustomSerializerFactory factory = new CustomSerializerFactory();
- factory.addGenericMapping(Date.class, new JsonSerializer<Date>(){
- @Override
- public void serialize(Date value,
- JsonGenerator jsonGenerator,
- SerializerProvider provider)
- throws IOException, JsonProcessingException {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- jsonGenerator.writeString(sdf.format(value));
- }
- });
- this.setSerializerFactory(factory);
- }
- }
spring-servlet.xml中配置:
- <mvc:annotation-driven>
- <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="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.