由于日期跳过Rest API请求上的Gson序列化而添加时间注释

在我的Java应用程序中,我使用的是REST API,它以JSON格式返回数据,并注意到该特定API以一种特殊的方式格式化了它的日期:“ Nov 1,2019”,但是问题是服务器上的实际日期是“ 2019-11-02”。这意味着我正在将日期最小化到以前的日期。我已经使用UTC时区格式为Date和Timestamp值添加了gson序列化,但是我注意到的是Date值跳过了序列化部分代码和只有时间戳记值使用序列化。我使用@Temporal(TemporalType.DATE)作为日期值,为什么它跳过日期序列化。我的服务器在另一个国家。下面是格式化后得到的完整json。

jsonaccts [{"id":8,"userId":2,"departmentId":45,"effectiveFrom":"Jun 9,2019","endsOn":"Nov 1,"createdBy":2,"createdOn":"2019-11-02 05:34:11"}]

下面是我用于gson格式化的代码。

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,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;

此处api请求中的builder.registerTypeAdapter(Date.class,new JsonSerializer<Date>() {}effectiveFrom未使用endsOn。 下面是ClassTeacher类。

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;
        }
    }

这里createdOn使用的是builder.registerTypeAdapter(Timestamp.class,new JsonSerializer<Timestamp>() {},但是effectiveFromendsOn没有使用序列化器。我在这里将'com.google.code.gson:gson:2.8.1'用于gson。 / p>

itdadao 回答:由于日期跳过Rest API请求上的Gson序列化而添加时间注释

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3168764.html

大家都在问