Spring uses Jackson JSON from http://jackson.codehaus.org/ to serialize an object for json output. To change the default strategy to serialize/format a field in a bean, you can easily create your own serializer.
Following is an example to customize date format in json oupt
1. Creat serializer
public class CustomDateSerializer extends JsonSerializer {
public static Log logger=LogFactory.getLog(CustomDateSerializer.class);
@Override
public void serialize(Date value, JsonGenerator gen, SerializerProvider arg2) throws IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
String formattedDate = formatter.format(value);
gen.writeString(formattedDate);
}
}
2. Annotate bean property to use the serializer
public class TransactionHistoryBean{
private Date date;
@JsonSerialize(using = CustomDateSerializer.class)
public Date getDate() {
return date;
}
}
Hi! Nice blog. We are also offering you QuickBooks Customer Service Phone Number. If you need any help regarding QuickBooks issues, dial 1-855-756-1077 for instant help.
ReplyDeleteyour blog is nice and useful
ReplyDeleteJava training in BTM,
Java institute in BTM,
Java training in BTM Layout,
Java institutes in Bangalore BTM,
Java training institutes in BTM Bangalore,
Java training in Bangalore BTM