Define the entity class first :

package com.zf.vo; import java.sql.Timestamp; import
com.fasterxml.jackson.annotation.JsonIgnore; import
com.fasterxml.jackson.databind.annotation.JsonSerialize; import
com.zf.control.JsonTimestampFormater; public class Person { private String name
; @JsonIgnore // Represents the conversion of the object to json String When , Ignore the property private int age ;
@JsonSerialize(using = JsonTimestampFormater.class)
// Represents the conversion of the object to Json When the object , The type converter used is your own defined converter private Timestamp birthday ; public
String getName() { return name; } public void setName(String name) {
= name; } public int getAge() { return age; } public void setAge(int age) {
this.age = age; } public Timestamp getBirthday() { return birthday; } public
void setBirthday(Timestamp birthday) { this.birthday = birthday; } }

The type converter is defined as follows :

package com.zf.control; import; import java.sql.Timestamp;
import java.text.SimpleDateFormat; 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
JsonTimestampFormater extends JsonSerializer<Timestamp> { private final
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); @Override
public void serialize(Timestamp value, JsonGenerator jgen, SerializerProvider
sp) throws IOException, JsonProcessingException {
jgen.writeString(sdf.format(value)); } }

The test classes are as follows :

package com.zf.test; import; import java.sql.Timestamp;
import com.fasterxml.jackson.core.JsonGenerationException; import
com.fasterxml.jackson.databind.JsonMappingException; import
com.fasterxml.jackson.databind.ObjectMapper; import com.zf.vo.Person; public
class Test { public static void main(String[] args) throws
JsonGenerationException, JsonMappingException, IOException { Person person =
new Person(); person.setAge(19); person.setName("xxxxx");
person.setBirthday(new Timestamp(System.currentTimeMillis())); ObjectMapper
mapper = new ObjectMapper(); String jsonstr =
mapper.writeValueAsString(person); System.out.println(jsonstr); } }

Print results

{"name":"xxxxx","birthday":"2012-08-03 03:05:06"}

©2020 ioDraw All rights reserved
Vue The difference between single page and multi page 0.96OLED display -4 Line SPI explain R In language Axis() Detailed explanation of function parameters JAVA Snake games ( Source code + notes )C++ vector Simulation Implementation of class javascript Adding and deleting form information CCF A series of questions --2016 year 4 Month 1 Calculation of break point postman Interface test get timestamp and MD5 encryption 【 Recommended benefits 】c++ use easyx Making pixel birds , Simple hands on games TCP/IP There are so many loopholes in the agreement ?