我从
GET
向 Postman
发送了 Tomcat v9.0
请求,其中包含以下数据。
{
"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
}
如下图。
为什么在我运行 Tomcat 服务器然后使用
GET
方法并使用 http://localhost:8080/HealthHelper/dietDiary/test
给定 url Postman
发送请求后,Eclipse IDE 中的控制台会打印以下输出?
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]
我做错了什么吗?
任何有关此问题的回复都将受到高度赞赏。
/HealthHelper/src/main/java/controller/TestController.java
使用Eclipse IDE,我在
/HealthHelper/src/main/java/controller/TestController.java
中用Java编写了Servlet代码,其中有一个带有部分url的WebServlet注释/dietDiary/test
-- @WebServlet("/dietDiary/test")
,如下所示。
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
在
/HealthHelper/src/main/java/vo/DietDiary2.java
中,有DietDiary2
满足JavaBean规则的公共类,如下所示。
请注意,我使用
java.sql.Timestamp
类来存储 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 + "]";
}
}
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:\Users\Jay\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706\jre
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.89\webapps
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:\Users\Jay\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;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:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Users\Jay\.jdks\openjdk-23\bin;C:\Users\Jay\AppData\Local\Microsoft\WindowsApps;C:\Users\Jay\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Jay\.jdks\openjdk-23\bin;;C:\Windows\System32;;.]
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.89\webapps\BookshopDemo_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.89\webapps\BookshopDemo_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.89\webapps\docs]
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:\apache-tomcat-9.0.89\webapps\docs] 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.89\webapps\examples]
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.89\webapps\examples] 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.89\webapps\FCMDemo_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.89\webapps\FCMDemo_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.89\webapps\host-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.89\webapps\host-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.89\webapps\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.89\webapps\manager] 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.89\webapps\ROOT]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:\apache-tomcat-9.0.89\webapps\ROOT] 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
发送带有 url GET
和
http://localhost:8080/HealthHelper/dietDiary/test
的
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:\Users\Jay\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706\jre
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.89\webapps
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:\Users\Jay\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_23.0.0.v20240919-1706\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;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:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\cmd;C:\Users\Jay\.jdks\openjdk-23\bin;C:\Users\Jay\AppData\Local\Microsoft\WindowsApps;C:\Users\Jay\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Jay\.jdks\openjdk-23\bin;;C:\Windows\System32;;.]
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.89\webapps\BookshopDemo_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.89\webapps\BookshopDemo_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.89\webapps\docs]
Oct 26, 2024 8:57:52 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:\apache-tomcat-9.0.89\webapps\docs] 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.89\webapps\examples]
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.89\webapps\examples] 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.89\webapps\FCMDemo_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.89\webapps\FCMDemo_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.89\webapps\host-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.89\webapps\host-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.89\webapps\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.89\webapps\manager] 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.89\webapps\ROOT]
Oct 26, 2024 8:57:53 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:\apache-tomcat-9.0.89\webapps\ROOT] 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]
我在本节中总结了其他信息(其中大部分都在上面提到过)。
localhost
8080
(Tomcat 中的默认端口号)。Postman
。我使用 Postman 来测试 Servlet。
http://localhost:8080/HealthHelper/dietDiary/test
GET
Eclipse
Java
我已检查这些设置是否正确。
关于信息,见上文。
javax.servlet
中的方法匹配。使用
GET
方法发送请求,当 url 匹配时将调用 doGet
方法(参见第 1 点)
此外,我已阅读这些文档。
从服务器接收到的
createTimestamp
的值(这里是Postman
)与服务器发送过来的json字符串中的keycreateTimestamp
对应的值一致。
更准确地说,我预料到了
Eclipse 中的控制台打印
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]
当发送带有以下数据的请求时。
{
"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
}
我知道为什么吗?
setDateFormat
GsonBuilder
课程中将为 java.util.Date
设置日期格式。
发帖后,我又仔细阅读了setDateFormat
类中的
API
GsonBuilder
,终于在下图中找到了线索。
setDateFormat
类中的 API GsonBuilder
的信息]]2
我单击带有日期文本的链接,它会重定向到有关 java.util.Date.
的网站。证明我的猜测是正确的。我尝试用手写一些代码。
首先,我创建一个文件
DietDiary3.java
。然后从DietDiary2.java
复制并粘贴。之后,我将名为 createTimestamp
的字段类型从 java.sql.Timestamp
更改为 java.util.Date
如下
公开课
DietDiary2
在DietDiary2.java
private Timestamp createTimestamp;
到
公开课
DietDiary3
在DietDiary3.java
private Date createTimestamp;
其次,我重构了 getter、setter 和
toString
方法。
第三,我在测试文件中使用
DietDiary3
代替 DietDiary2
-- /HealthHelper/src/main/java/controller/TestController.java
做到了
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));
}
}
最后,我保存它并运行
Tomcat
,然后再次发送请求(使用 Postman
)。
我在 Eclipse 打印中看到了控制台
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.
与请求中 json 字符串中名为
createTimestamp
的键对应的值一致。
createTimestamp":"2022-05-05 12:00:00",