SpringBoot @ JsonDeserialize Customize Json Serialization
- 2021-11-24 01:47:02
- OfStack
@ JsonDeserialize Custom Json Serialization
1. Problems
In the project, SpringBoot is used as the framework. When the third-party interface is called, the returned parameter Date type needs to be customized for Json serialization, processed and data accepted
2. Phenomena
Invoke the third-party interface and return a parameter of type Date in the following format:
{
"created": "2018-12-27 16:15:25",
"lastupd": "2018-12-27 08:25:48"
}
The format of returned Date type data is: yyyy-MM-dd HH: mm: ss, Json default serialization Date type parameter, format is: yyyy-MM-dd HH: mm: ss. SSS, then user-defined serialization is required
3. Solutions
Create the received data object and generate the Get\ Set method:, on top of the Set method, add the @ JsonDeserialize annotation, as follows:
public class TestDto implements Serializable {
/**
* Generation time
*
*/
private Date created;
/**
* LastUpd
*
*/
private Date lastUpd;
public Date getCreated() {
return created;
}
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
public void setCreated(Date created) {
this.created = created;
}
public Date getLastUpd() {
return lastUpd;
}
@JsonDeserialize(using = CustomJsonDateDeserializer.class)
public void setLastUpd(Date lastUpd) {
this.lastUpd = lastUpd;
}
}
When doing custom serialization, add the @ JsonDeserialize (using = CustomJsonDateDeserializer. class) annotation,
Where @ JsonDeserialize means telling SpringBoot that the current attribute is custom serialized, then SpringBoot will skip this parameter when serializing
CustomJsonDateDeserializer. class is a custom serialization object, as follows:
package com.test;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Custom serialization
**/
public class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
@Override
public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = jp.getText();
try {
return format.parse(date);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
Above, when receiving data, Json serialization will be customized to receive data in Date format.
Using @ JsonSerialize with @ JsonDeserialize
1. Use as an annotation
@JsonDeserialize(using= DateJsonDeserializer.class)
@JsonSerialize(using= DateJsonSerializer.class)
private Date time;
2. Custom implementation classes
public class DateJsonDeserializer extends JsonDeserializer<Date>
{
public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public Date deserialize(com.fasterxml.jackson.core.JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, com.fasterxml.jackson.core.JsonProcessingException {
try
{
if(jsonParser!=null&&StringUtils.isNotEmpty(jsonParser.getText())){
return format.parse(jsonParser.getText());
}else {
return null;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
throw new RuntimeException(e);
}
}
}
public class DateJsonSerializer extends JsonSerializer<Date> {
public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(format.format(date));
}