skip to Main Content

What I do?

I sent a GET request from Postman to Tomcat v9.0 with following data.

{
    "diaryId":2,
    "userId":2,
    "createTimestamp":"2022-05-05 12:00:00",  
    "totalFat":2.52,
    "totalCarbon":2.3,
    "totalProtein":2.1,
    "totalFiber":2.1,
    "totalSugar":1.2,
    "totalSodium":1.1,
    "totalCalories":1.21 
}

figure as follows.

GET request from Postman to Tomcat v9.0

Why does the Console in Eclipse IDE prints the following output after I run the Tomcat server then sending a request with GET method and given url http://localhost:8080/HealthHelper/dietDiary/test using Postman?

Ready to deserialize.
dietDiary2:DietDiary2 [diaryId=2, userId=2, createTimestamp=2021-12-26 00:00:00.0, totalFat=2.52, totalCarbon=2.3, totalProtein=2.1, totalFiber=2.1, totalSugar=1.2, totalSodium=1.1, totalCalories=1.21]

Does I do some wrong?

Appreciation

Any replies about this issue will be highly appreciated.

Code

/HealthHelper/src/main/java/controller/TestController.java

With Eclipse IDE I wrote a Servlet code in Java in /HealthHelper/src/main/java/controller/TestController.java, there are a WebServlet annotation with part of url /dietDiary/test@WebServlet("/dietDiary/test") shown as follows.

package controller;

import java.io.IOException;

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 constant.DatePattern;
import vo.DietDiary2;

@WebServlet("/dietDiary/test")
public class TestController extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
    
    @Override 
    protected void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException{
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder
            .setDateFormat(DatePattern.datePattern);
        Gson gson = gsonBuilder.create();
        System.out.println("Ready to deserialize.");
        DietDiary2 dietDiary2 = gson.fromJson(req.getReader(), DietDiary2.class);
        System.out.println("dietDiary2:"+dietDiary2.toString());
    }
}

/HealthHelper/src/main/java/constant/DatePattern.java

package constant;

public class DatePattern {
    public final static String datePattern = "YYYY-MM-DD hh:mm:ss";
}

/HealthHelper/src/main/java/vo/DietDiary2.java

In /HealthHelper/src/main/java/vo/DietDiary2.java, there are DietDiary2 public class that satsifies the JavaBean rule, shown as follows.

Notice that I use java.sql.Timestamp class to store Timestamp.

package vo;

import java.sql.Timestamp;

public class DietDiary2 {
    private int diaryId;
    private int userId;
    private Timestamp createTimestamp;
    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 Timestamp getCreateTimestamp() {
        return createTimestamp;
    }
    public void setCreateTimestamp(Timestamp createTimestamp) {
        this.createTimestamp = createTimestamp;
    }
    
    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 DietDiary2) {
            return false;
        }
        
        DietDiary2 dietDiary = (DietDiary2) object;
        
        if(!(this.getDiaryId() == dietDiary.getDiaryId())) {
            return false;
        }
        
        if(!(this.getUserId() == dietDiary.getUserId())) {
            return false;
        }
        
        if(!(this.getCreateTimestamp() == dietDiary.getCreateTimestamp())) {
            return false;
        }
        
        return true;
    }
    @Override
    public String toString() {
        return "DietDiary2 [diaryId=" + diaryId + ", userId=" + userId + ", createTimestamp=" + createTimestamp
                + ", totalFat=" + totalFat + ", totalCarbon=" + totalCarbon + ", totalProtein=" + totalProtein
                + ", totalFiber=" + totalFiber + ", totalSugar=" + totalSugar + ", totalSodium=" + totalSodium
                + ", totalCalories=" + totalCalories + "]";
    }

    
}

info at Console in Eclipse IDE.

info at Console when start execution of Tomcat v9.0.

Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 3 2024 20:22:11 UTC
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.89.0
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 11
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:UsersJay.p2poolpluginsorg.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706jre
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           23+37
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Eclipse Adoptium
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:apache-tomcat-9.0.89webapps
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dstdout.encoding=UTF-8
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dstderr.encoding=UTF-8
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
Oct 26, 2024 8:57:50 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:UsersJay.p2poolpluginsorg.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706jrebin;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:/Users/Jay/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706/jre/bin/server;C:/Users/Jay/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706/jre/bin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program FilesNVIDIA CorporationNVIDIA NvDLISR;C:ProgramDatachocolateybin;C:Program FilesGitcmd;C:UsersJay.jdksopenjdk-23bin;C:UsersJayAppDataLocalMicrosoftWindowsApps;C:UsersJayAppDataLocalProgramsMicrosoft VS Codebin;C:UsersJay.jdksopenjdk-23bin;;C:WindowsSystem32;;.]
Oct 26, 2024 8:57:50 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [486] milliseconds
Oct 26, 2024 8:57:51 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Oct 26, 2024 8:57:51 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.89]
Oct 26, 2024 8:57:51 PM org.apache.tomcat.util.descriptor.web.WebXml setVersion
WARNING: Unknown version string [5.0]. Default version will be used.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Oct 26, 2024 8:57:52 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsBookshopDemo_Server]
Oct 26, 2024 8:57:52 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsBookshopDemo_Server] has finished in [38] ms
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsdocs]
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsdocs] has finished in [15] ms
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsexamples]
Oct 26, 2024 8:57:52 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Oct 26, 2024 8:57:52 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Oct 26, 2024 8:57:52 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@619f2afc')
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsexamples] has finished in [196] ms
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsFCMDemo_Server]
Oct 26, 2024 8:57:53 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsFCMDemo_Server] has finished in [1,435] ms
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappshost-manager]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappshost-manager] has finished in [15] ms
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsmanager]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsmanager] has finished in [13] ms
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsROOT]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsROOT] has finished in [10] ms
Oct 26, 2024 8:57:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 26, 2024 8:57:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [3024] milliseconds

info at Console after sent GET request with url http://localhost:8080/HealthHelper/dietDiary/test with Postman.

Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name:   Apache Tomcat/9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          May 3 2024 20:22:11 UTC
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 9.0.89.0
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 11
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            10.0
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:UsersJay.p2poolpluginsorg.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706jre
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           23+37
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Eclipse Adoptium
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:apache-tomcat-9.0.89
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:apache-tomcat-9.0.89webapps
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dstdout.encoding=UTF-8
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dstderr.encoding=UTF-8
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -XX:+ShowCodeDetailsInExceptionMessages
Oct 26, 2024 8:57:50 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:UsersJay.p2poolpluginsorg.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706jrebin;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:/Users/Jay/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706/jre/bin/server;C:/Users/Jay/.p2/pool/plugins/org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706/jre/bin;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program FilesNVIDIA CorporationNVIDIA NvDLISR;C:ProgramDatachocolateybin;C:Program FilesGitcmd;C:UsersJay.jdksopenjdk-23bin;C:UsersJayAppDataLocalMicrosoftWindowsApps;C:UsersJayAppDataLocalProgramsMicrosoft VS Codebin;C:UsersJay.jdksopenjdk-23bin;;C:WindowsSystem32;;.]
Oct 26, 2024 8:57:50 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Oct 26, 2024 8:57:50 PM org.apache.catalina.startup.Catalina load
INFO: Server initialization in [486] milliseconds
Oct 26, 2024 8:57:51 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Oct 26, 2024 8:57:51 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.89]
Oct 26, 2024 8:57:51 PM org.apache.tomcat.util.descriptor.web.WebXml setVersion
WARNING: Unknown version string [5.0]. Default version will be used.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Oct 26, 2024 8:57:52 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsBookshopDemo_Server]
Oct 26, 2024 8:57:52 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsBookshopDemo_Server] has finished in [38] ms
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsdocs]
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsdocs] has finished in [15] ms
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsexamples]
Oct 26, 2024 8:57:52 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Oct 26, 2024 8:57:52 PM org.apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Oct 26, 2024 8:57:52 PM org.apache.catalina.core.ApplicationContext log
INFO: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@619f2afc')
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsexamples] has finished in [196] ms
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsFCMDemo_Server]
Oct 26, 2024 8:57:53 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsFCMDemo_Server] has finished in [1,435] ms
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappshost-manager]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappshost-manager] has finished in [15] ms
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsmanager]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsmanager] has finished in [13] ms
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory [C:apache-tomcat-9.0.89webappsROOT]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:apache-tomcat-9.0.89webappsROOT] has finished in [10] ms
Oct 26, 2024 8:57:54 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Oct 26, 2024 8:57:54 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [3024] milliseconds
Ready to deserialize.
dietDiary2:DietDiary2 [diaryId=2, userId=2, createTimestamp=2021-12-26 00:00:00.0, totalFat=2.52, totalCarbon=2.3, totalProtein=2.1, totalFiber=2.1, totalSugar=1.2, totalSodium=1.1, totalCalories=1.21]

other info

I summary the other info (which most of them are mentioned above) in this section.

Server

Backend Server

Tomcat v9.0 (version 9.0)

Overview of settings about Tomcat

  1. version is 9.0
  2. IP address is localhost
  3. port number is 8080 (default port number in Tomcat).

Overview of settings about Tomcat

Frontend Server

Postman. I use Postman to test the Servlet.

request
url

http://localhost:8080/HealthHelper/dietDiary/test

method

GET

IDE

Eclipse

Programming language

Java

Referenced Library under HealthHelper project

Referenced Library

What did I try?

I have check these settings are correct.

  1. url of request:

About Info, see above.

  1. method of request DOES match the method in javax.servlet.

Sending a request with GET method will call the doGet method when the url does match (see the 1. point)

Additionally, I have read these docs.

  1. javax.servlet.http.HttpServlet class

  2. API about setDateFormat nonsttic method in com.google.gson.GsonBuilder class

What did I expected?

The value of createTimestamp that is received from the server (here is Postman) is consistent with corresponding value of key createTimestamp in json string that is sent from the server.

More exactly to say, I expected that

The console in Eclipse prints

Ready to deserialize.
dietDiary2:DietDiary2 [diaryId=2, userId=2, createTimestamp=2022-05-05 12:00:00, totalFat=2.52, totalCarbon=2.3, totalProtein=2.1, totalFiber=2.1, totalSugar=1.2, totalSodium=1.1, totalCalories=1.21]

when a request is sent with following data.

{
    "diaryId":2,
    "userId":2,
    "createTimestamp":"2022-05-05 12:00:00",  
    "totalFat":2.52,
    "totalCarbon":2.3,
    "totalProtein":2.1,
    "totalFiber":2.1,
    "totalSugar":1.2,
    "totalSodium":1.1,
    "totalCalories":1.21 
}

2

Answers


  1. Chosen as BEST ANSWER

    I finally know why!!!

    setDateFormat in GsonBuilder class will set date format for java.util.Date.

    After posting, I read the API setDateFormat in GsonBuilder class again carefully, I finally found the clue on the following figure.

    ![info about API setDateFormat in GsonBuilder class]]2

    I click the link with Date text and it redirect to a wbesite about java.util.Date.

    To prove my guess is true. I try to write some code by my hand.

    First, I create a file DietDiary3.java. Then copy and pasted from DietDiary2.java. After that I change the type of field named createTimestamp from java.sql.Timestamp to java.util.Date as follows

    In public class DietDiary2 in DietDiary2.java

    private Timestamp createTimestamp; 
    

    to

    In public class DietDiary3 in DietDiary3.java

    private Date createTimestamp;
    

    Second, I refactored the getter, setter and toString method.

    Third, I use DietDiary3 instead DietDiary2 in test file -- /HealthHelper/src/main/java/controller/TestController.java

    Making it to

    package controller;
    
    import java.io.IOException;
    import java.sql.Timestamp;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    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 constant.DatePattern;
    import vo.DietDiary2;
    import vo.DietDiary3;
    
    @WebServlet("/dietDiary/test")
    public class TestController extends HttpServlet {
        
        private static final long serialVersionUID = 1L;
        
        @Override 
        protected void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException{
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder
                .setDateFormat(DatePattern.datePattern);
            Gson gson = gsonBuilder.create();
            System.out.println("Ready to deserialize.");
            DietDiary3 dietDiary3 = gson.fromJson(req.getReader(), DietDiary3.class);
            System.out.println("dietDiary3:"+dietDiary3.toString());
            Date createTimestamp = dietDiary3.getCreateTimestamp();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DatePattern.datePattern);
            System.out.printf("simpleDateFormat.format(%s):%s.%n",createTimestamp,simpleDateFormat.format(createTimestamp));
        }
    }
    

    Last, I save it and run the Tomcat then send the request (using Postman) again.

    And I see the console in Eclipse prints

    Ready to deserialize.
    dietDiary3:DietDiary3 [diaryId=2, userId=2, createTimestamp=Sun Dec 26 00:00:00 CST 2021, totalFat=2.52, totalCarbon=2.3, totalProtein=2.1, totalFiber=2.1, totalSugar=1.2, totalSodium=1.1, totalCalories=1.21]
    simpleDateFormat.format(Sun Dec 26 00:00:00 CST 2021):2022-12-360 12:00:00.
    

    which is consistent with corresponding value to key named createTimestamp in a json string within the request.

    createTimestamp":"2022-05-05 12:00:00",
    

  2. The problem might be your date format. As mentioned by Gson’s GsonBuilder#setDateFormat(String), the format is the one used by SimpleDateFormat.

    The problems with your pattern YYYY-MM-DD hh:mm:ss are:

    • Y: is ‘week year’, most likely you want to use lowercased y instead, see for example https://errorprone.info/bugpattern/MisusedWeekYear
    • D: is ‘day in year’, most likely you wanted to use lowercased d (‘day in month’)
    • h: is ‘hour in am/pm (1-12)’, since your date pattern does not include an "AM/PM" marker most likely you wanted to use uppercased H (‘hour in day (0-23)’)

    So the correct pattern would be:

    yyyy-MM-dd HH:mm:ss
    

    Probably due to the incorrect pattern, and lenient parsing done by SimpleDateFormat, you get incorrect date results.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search