issue
intro
Why are they sometimes inconsistent? value of java.sql.Date
type received from server Postman
then is deserialized by com.google.gson.Gson
(and before instantiating the Gson instance, I register two adapters -- one for deserializing java.sql.Date
, another for deserializing java.sql.Time
.). And a json string sent from server.
I try to sent a request from Postman
to Tomcat v9.0
and deserialized the raw data (here is a json string) to DietDiary
object which contain two fields with type java.sql.Date
and java.sql.Time
using com.google.gson
package.
Two keys of raw data of request in Postman
.
"createDate":"2022-05-05","createTime":"12:00:00",
But the Console in Eclipse prints
Ready to deserialize.dietDiary:DietDiary [diaryId=2, userId=2, createDate=2021-12-26, createTime=00:00:00, totalFat=2.52, totalCarbon=2.3, totalProtein=2.1, totalFiber=2.1, totalSugar=1.2, totalSodium=1.1, totalCalories=1.21]
I don't know why?
Appreciation
Any reply about this issue will be highly appreciated.
detailed info
Here is detailed info.
server
name
Postman
request
method
GET
url
http://localhost:8080/HealthHelper/dietDiary/test
raw data
A json string.
{"diaryId":2,"userId":2,"createDate":"2022-05-05","createTime":"12:00:00", "totalFat":2.52,"totalCarbon":2.3,"totalProtein":2.1,"totalFiber":2.1,"totalSugar":1.2,"totalSodium":1.1,"totalCalories":1.21 }
Client
Tomcat
v9.0 (version 9)
Code
In /HealthHelper/src/main/java/controller/TestController.java
package controller;import java.io.IOException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.sql.Date;import java.sql.Time;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.JsonDeserializationContext;import com.google.gson.JsonDeserializer;import com.google.gson.JsonElement;import com.google.gson.JsonObject;import com.google.gson.JsonParseException;import constant.SqlDatePattern;import constant.SqlTimePattern;import util.gson.GsonForSqlDateAndSqlTime;import util.gson.deserializer.JsonDeserializerForSqTime;import util.gson.deserializer.JsonDeserializerForSqlDate;import vo.DietDiary;@WebServlet("/dietDiary/test")public class TestController extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { Gson gson = GsonForSqlDateAndSqlTime.gson; DietDiary dietDiary = gson.fromJson(req.getReader(), DietDiary.class); System.out.println("Ready to deserialize."); System.out.println("dietDiary:"+dietDiary); }}
In /HealthHelper/src/main/java/vo/DietDiary.java
A class that satisfies JavaBean rule and not be polluted.
package vo;import java.sql.Date;import java.sql.Time;public class DietDiary { private int diaryId; private int userId; private Date createDate; private Time createTime; private Double totalFat; private Double totalCarbon; private Double totalProtein; private Double totalFiber; private Double totalSugar; private Double totalSodium; private Double totalCalories; public int getDiaryId() { return diaryId; } public void setDiaryId(int diaryId) { this.diaryId = diaryId; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Time getCreateTime() { return createTime; } public void setCreateTime(Time createTime) { this.createTime = createTime; } public Double getTotalFat() { return totalFat; } public void setTotalFat(Double totalFat) { this.totalFat = totalFat; } public Double getTotalCarbon() { return totalCarbon; } public void setTotalCarbon(Double totalCarbon) { this.totalCarbon = totalCarbon; } public Double getTotalProtein() { return totalProtein; } public void setTotalProtein(Double totalProtein) { this.totalProtein = totalProtein; } public Double getTotalFiber() { return totalFiber; } public void setTotalFiber(Double totalFiber) { this.totalFiber = totalFiber; } public Double getTotalSugar() { return totalSugar; } public void setTotalSugar(Double totalSugar) { this.totalSugar = totalSugar; } public Double getTotalSodium() { return totalSodium; } public void setTotalSodium(Double totalSodium) { this.totalSodium = totalSodium; } public Double getTotalCalories() { return totalCalories; } public void setTotalCalories(Double totalCalories) { this.totalCalories = totalCalories; } public boolean equals(Object object) { if(object instanceof DietDiary) { return false; } DietDiary dietDiary = (DietDiary) object; if(!(this.getDiaryId() == dietDiary.getDiaryId())) { return false; } if(!(this.getUserId() == dietDiary.getUserId())) { return false; } if(!(this.getCreateDate() == dietDiary.getCreateDate())) { return false; } if(!(this.getCreateTime() == dietDiary.getCreateTime())) { return false; } return true; } @Override public String toString() { return "DietDiary [diaryId=" + diaryId +", userId=" + userId +", createDate=" + createDate +", createTime="+ createTime +", totalFat=" + totalFat +", totalCarbon=" + totalCarbon +", totalProtein="+ totalProtein +", totalFiber=" + totalFiber +", totalSugar=" + totalSugar +", totalSodium="+ totalSodium +", totalCalories=" + totalCalories +"]"; }}
In /HealthHelper/src/main/java/util/gson/GsonForSqlDateAndSqlTime.java
In I put a gson instance whose adapters are registered for deserialized java.sql.Date
and java.sql.Time
in GsonForSqlDateAndSqlTime
public class
package util.gson;import java.sql.Date;import java.sql.Time;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import util.gson.deserializer.JsonDeserializerForSqTime;import util.gson.deserializer.JsonDeserializerForSqlDate;public class GsonForSqlDateAndSqlTime { public static Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, JsonDeserializerForSqlDate.dateDeserializer) .registerTypeAdapter(Time.class, JsonDeserializerForSqTime.timeDeserializer) .create();}
In /HealthHelper/src/main/java/util/gson/deserializer/JsonDeserializerForSqlDate.java
package util.gson.deserializer;import java.lang.reflect.Type;import java.sql.Date;import java.text.ParseException;import java.text.SimpleDateFormat;import com.google.gson.JsonDeserializationContext;import com.google.gson.JsonDeserializer;import com.google.gson.JsonElement;import com.google.gson.JsonParseException;import constant.SqlDatePattern;public class JsonDeserializerForSqlDate { public static JsonDeserializer<Date> dateDeserializer = new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat dateFormat = new SimpleDateFormat(SqlDatePattern.sqlDatePattern); try { return new Date(dateFormat.parse(json.getAsString()).getTime()); } catch (ParseException e) { throw new JsonParseException(e); } } };}
In /HealthHelper/src/main/java/util/gson/deserializer/JsonDeserializerForSqTime.java
package util.gson.deserializer;import java.lang.reflect.Type;import java.sql.Time;import java.text.ParseException;import java.text.SimpleDateFormat;import com.google.gson.JsonDeserializationContext;import com.google.gson.JsonDeserializer;import com.google.gson.JsonElement;import com.google.gson.JsonParseException;import constant.SqlTimePattern;public class JsonDeserializerForSqTime { public static JsonDeserializer<Time> timeDeserializer = new JsonDeserializer<Time>() { @Override public Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { SimpleDateFormat timeFormat = new SimpleDateFormat(SqlTimePattern.sqlTimePattern); try { return new Time(timeFormat.parse(json.getAsString()).getTime()); } catch (ParseException e) { throw new JsonParseException(e); } } };}
What did I try?
I have read these docs.