The first type:
JSON uses this dependency.
<!--JSON Lib development package and its dependency package--
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
When SPRINGMVC returns JSON data, the default Date field displays a long type of timestamp
If you want to return a formatted date such as: YYYY-MM-DD This format needs to appear
Here's how:
Write a single processing class inheritance Jsonserializer
Code
12345678910111213141516171819 |
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
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));
}
}
|
Add a @jsonserialize annotation to an attribute field on an entity class
Code
12 |
@JsonSerialize (using = CustomDateSerializer. class ) private Date createTime; |
The second type:
This package supports the case
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
SPRINGMVC date format for returning JSON when using @responsebody
Prerequisites to understand: @ResponseBody The core class that returns the JSON string is Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter, which uses the open-source third-party class library of Jackson. The main is the following two jar packages: Jackson-core-asl-1.6.4.jar;jackson-mapper-asl-1.6.4.jar.
Problem: Returns the date format of the JSON string when using @responsebody. The Date Type property returns a long timestamp by default, and how can I return a custom date format?
Solution: There are two ways to achieve this,
1, local modification (online more, but not recommended);
Inherit Jackson's abstract class: Jsonserializer<t>, and then add annotations @jsonserialize on JavaBean's property getter () to implement.
The code is as follows:
Java code
- 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 Custom Returns the format of the date in the JSON data grid processing
- * @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:
Java code
- @JsonSerialize (using = Customdateserializer. Class)
- Public Date getcreatedate () {
- return createdate;
- }
2. Global modification (highly recommended):
The Mappingjacksonhttpmessageconverter is primarily implemented by Objectmapper to return a JSON string. Here we inherit the class, registering a jsonserializer<t>. The custom objectmapper is then injected into the configuration file.
The code is as follows:
Java code
- 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 Resolve Date type return JSON format to custom format
- * @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);
- }
- }
Configuration in Spring-servlet.xml:
Java code
- <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, @DatetimeFormat the use of attention
1, the use of @datetimeformat is very simple, it should be noted here: use to introduce a class library Joda-time-1.3.jar, otherwise you will not be able to access the corresponding path (400 error).
PS: This annotation can work on Method,field and parameter levels.
Reference for use: http://www.captaindebug.com/2011/08/using-spring-3-datetimeformat.html#.UaR3mWWZk0k
2. Because SPRINGMVC does not provide a default date converter, the previous page passes over how the date string is converted to a date type, and can be done using the @datetimeformat annotation if no global date converter or data binding is provided.
SPINGMVC returns the JSON data date formatting method