问题
In my java application i was using REST API that returned data in JSON format, and noticed that this particular API formatted it's dates in a peculiar way: "Nov 1, 2019" , But problem is that the actual date on the server is "2019-11-02".That means I am getiing date minimized to previous date.I have added gson serialization for both Date and Timestamp values using UTC timezone format, but what i noted is the Date values skip the serialization part on the code and only Timestamp values are using the serialization.I have used @Temporal(TemporalType.DATE) for date values that why it skips the date serialization.My server is on another country.Below is the complete json that i got after formatting.
jsonAccts [{"id":8,"userId":2,"departmentId":45,"effectiveFrom":"Jun 9, 2019","endsOn":"Nov 1, 2019","createdBy":2,"createdOn":"2019-11-02 05:34:11"}]
Below is the code that i used for gson formatting.
Gson gson;
GsonBuilder builder;
SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat dtfDate=new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String jsonAccts = null;
try{
builder = new GsonBuilder();
builder.registerTypeAdapter(Timestamp.class, new JsonSerializer<Timestamp>() {
@Override
public JsonElement serialize(Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
dtf.setTimeZone(TimeZone.getTimeZone("UTC"));
String jsDate = dtf.format(src);
return new JsonPrimitive(jsDate);
}
});
builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
dtf.setTimeZone(TimeZone.getTimeZone("UTC"));
String jsDate = dtf.format(src);
return new JsonPrimitive(jsDate);
}
});
gson = builder.create();
List<ClassTeacher> allActPgmMap = new ArrayList<ClassTeacher>();
allActPgmMap = springDao.getClassTeacherList(Integer.parseInt(deptId));
Type listType = new TypeToken<List<ClassTeacher>>() {}.getType();
jsonAccts = gson.toJson(allActPgmMap, listType);
}catch(Exception e){
e.printStackTrace();
}
return jsonAccts;
Here the builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {}
is been not used for effectiveFrom
and endsOn
on api request.
Below is the ClassTeacher class.
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
@Entity
@Table(name = "class_teacher")
public class ClassTeacher implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "user_id")
private long userId;
@Column(name = "department_id")
private long departmentId;
@Column(name = "effective_from")
@Temporal(TemporalType.DATE)
private Date effectiveFrom;
@Column(name = "ends_on")
@Temporal(TemporalType.DATE)
private Date endsOn;
@Column(name="created_by")
private long createdBy;
@Column(name="created_on")
private Timestamp createdOn;
public ClassTeacher() {
}
public ClassTeacher(long id, long departmentId, long userId, Date effectiveFrom, Date endsOn, long createdBy, Timestamp createdOn) {
this.id = id;
this.departmentId = departmentId;
this.userId = userId;
this.effectiveFrom = effectiveFrom;
this.endsOn = endsOn;
this.createdBy = createdBy;
this.createdOn = createdOn;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public long getDepartmentId() {
return departmentId;
}
public void setDepartmentId(long departmentId) {
this.departmentId = departmentId;
}
public Date getEffectiveFrom() {
return effectiveFrom;
}
public void setEffectiveFrom(Date effectiveFrom) {
this.effectiveFrom = effectiveFrom;
}
public Date getEndsOn() {
return endsOn;
}
public void setEndsOn(Date endsOn) {
this.endsOn = endsOn;
}
public long getCreatedBy() {
return createdBy;
}
public void setCreatedBy(long createdBy) {
this.createdBy = createdBy;
}
public Timestamp getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Timestamp createdOn) {
this.createdOn = createdOn;
}
}
Here the createdOn
is using the builder.registerTypeAdapter(Timestamp.class, new JsonSerializer<Timestamp>() {}
,but effectiveFrom
and endsOn
are not using the serializer.I have used 'com.google.code.gson:gson:2.8.1'
here for gson.
来源:https://stackoverflow.com/questions/58688079/add-temporal-annoation-as-date-skips-gson-serialization-on-rest-api-request