issue
Why do I get the response code 200 when request sent from Postman to Tomcat server in my project. but get 404 in another project?
To my project HealthHelper
In my project HealthHelper
,
I ran the Tomcat Server first.
I sent a request with following url and raw data from Postman to Tomcat Server v9.0.
I receive the request and handles it with
Java
code withjavax.servlet
package.
url
http://localhost:8080/HealthHelper/dietDiary/query/byTime
raw data
{"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 }
output in Eclipse Console
Then I get output in Eclipse Console that I expected.
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]
Code
Here is /HealthHelper/src/main/java/web/dietdiary/controller/QueryDietDiaryByDateAndTimeController.java
in my project.
package web.dietdiary.controller;import java.io.IOException;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.util.ArrayList;import javax.naming.NamingException;import javax.servlet.ServletException;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.JsonObject;import web.dietdiary.constant.SqlDatePattern;import web.dietdiary.service.impl.DietDiaryService;import web.dietdiary.service.impl.DietDiaryServiceImpl;import web.dietdiary.util.datetime.DateTimeHandler;import web.dietdiary.util.datetime.DateTimeHandlerImpl;import web.dietdiary.util.gson.GsonForSqlDateAndSqlTime;import web.dietdiary.vo.DietDiary;@WebServlet("/dietDiary/query/byTime")public class QueryDietDiaryByDateAndTimeController extends HttpServlet { private static final long serialVersionUID = 1L; private DietDiaryService dietDiaryService; @Override public void init() throws ServletException { try { this.dietDiaryService = new DietDiaryServiceImpl(null); } catch (NamingException e) { e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException{ Gson gson = GsonForSqlDateAndSqlTime.gson; JsonObject jsonObject = new JsonObject(); String errorMessage = ""; String result = ""; int affectedRow = 0; ArrayList<DietDiary> dietDiaries = new ArrayList<DietDiary>(); DietDiary dietDiary = gson.fromJson(req.getReader(), DietDiary.class); System.out.println("Ready to deserialize."); System.out.println("dietDiary:"+dietDiary); dietDiaries = this.dietDiaryService.search(dietDiary,2); if(dietDiaries == null) { errorMessage = "Unknown error!!!"; affectedRow = -1; jsonObject.addProperty("result", false); jsonObject.addProperty("affectedRow", affectedRow); jsonObject.addProperty("errorMessage", errorMessage); res.getWriter().write(jsonObject.toString()); return; } if(dietDiaries.isEmpty()){ errorMessage = ""; result = "not found."; affectedRow = 0; jsonObject.addProperty("result", result); jsonObject.addProperty("affectedRow", affectedRow); jsonObject.addProperty("errorMessage", errorMessage); res.getWriter().write(jsonObject.toString()); return; } result = ""; result += "["; result += "\n"; for(int i=0;i<dietDiaries.size();i++) { DietDiary tempDietDiary = dietDiaries.get(i); result += tempDietDiary.toString(); result += "\n"; } result += "]"; result += "\n"; affectedRow = dietDiaries.size(); errorMessage = ""; jsonObject.addProperty("result", result); jsonObject.addProperty("affectedRow", affectedRow); jsonObject.addProperty("errorMessage", errorMessage); res.getWriter().write(jsonObject.toString()); return; }}
other info
project facet
Java Build Path
To other project HealthHelperServer
(which is created by other members of the group)
A few days ago, I copy and pasted the QueryDietDiaryByTimeController.java
from QueryDietDiaryByTimeController.java
(full qualified name is /HealthHelper/src/main/java/web/dietdiary/controller/QueryDietDiaryByTimeController.java
) in my project to QueryDietDiaryByTimeController.java
(full qualified name is /HealthyHelperServer/src/main/java/web/dietdiary/controller/QueryDietDiaryByDateAndTimeController.java
)in other project.
In other project HealthHelperServer
, I do same thing to in other project.
I ran the Tomcat Server first.
I sent a request with following url and raw data from Postman to Tomcat Server v9.0.
I receive the request and handles it with
Java
code withjavax.servlet
package.
url
http://localhost:8080/HealthHelperServer/dietDiary/query/byTime
raw data
{"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 }
output in Eclipse Console
Then I get nothing in Eclipse Console.
But I got a 404 NOT found
error in Postman
.
Body
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css"> body { font-family: Tahoma, Arial, sans-serif; } h1, h2, h3, b { color: white; background-color: #525D76; } h1 { font-size: 22px; } h2 { font-size: 16px; } h3 { font-size: 14px; } p { font-size: 12px; } a { color: black; } .line { height: 1px; background-color: #525D76; border: none; }</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> The requested resource [/HealthHelperServer/dietDiary/query/byTime] is not available</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.89</h3></body></html>
Code
Here is /HealthyHelperServer/src/main/java/web/dietdiary/controller/QueryDietDiaryByDateAndTimeController.java
in other project.
package web.dietdiary.controller;import java.io.IOException;import java.sql.Date;import java.sql.Time;import java.sql.Timestamp;import java.util.ArrayList;import javax.naming.NamingException;import javax.servlet.ServletException;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.JsonObject;import web.dietdiary.constant.SqlDatePattern;import web.dietdiary.service.impl.DietDiaryService;import web.dietdiary.service.impl.DietDiaryServiceImpl;import web.dietdiary.util.datetime.DateTimeHandler;import web.dietdiary.util.datetime.DateTimeHandlerImpl;import web.dietdiary.util.gson.GsonForSqlDateAndSqlTime;import web.dietdiary.vo.DietDiary;@WebServlet("/dietDiary/query/byTime")public class QueryDietDiaryByDateAndTimeController extends HttpServlet { private static final long serialVersionUID = 1L; private DietDiaryService dietDiaryService; @Override public void init() throws ServletException { try { this.dietDiaryService = new DietDiaryServiceImpl(null); } catch (NamingException e) { e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException{ Gson gson = GsonForSqlDateAndSqlTime.gson; JsonObject jsonObject = new JsonObject(); String errorMessage = ""; String result = ""; int affectedRow = 0; ArrayList<DietDiary> dietDiaries = new ArrayList<DietDiary>(); DietDiary dietDiary = gson.fromJson(req.getReader(), DietDiary.class); System.out.println("Ready to deserialize."); System.out.println("dietDiary:"+dietDiary); dietDiaries = this.dietDiaryService.search(dietDiary,2); if(dietDiaries == null) { errorMessage = "Unknown error!!!"; affectedRow = -1; jsonObject.addProperty("result", false); jsonObject.addProperty("affectedRow", affectedRow); jsonObject.addProperty("errorMessage", errorMessage); res.getWriter().write(jsonObject.toString()); return; } if(dietDiaries.isEmpty()){ errorMessage = ""; result = "not found."; affectedRow = 0; jsonObject.addProperty("result", result); jsonObject.addProperty("affectedRow", affectedRow); jsonObject.addProperty("errorMessage", errorMessage); res.getWriter().write(jsonObject.toString()); return; } result = ""; result += "["; result += "\n"; for(int i=0;i<dietDiaries.size();i++) { DietDiary tempDietDiary = dietDiaries.get(i); result += tempDietDiary.toString(); result += "\n"; } result += "]"; result += "\n"; affectedRow = dietDiaries.size(); errorMessage = ""; jsonObject.addProperty("result", result); jsonObject.addProperty("affectedRow", affectedRow); jsonObject.addProperty("errorMessage", errorMessage); res.getWriter().write(jsonObject.toString()); return; }}
other infos
project facets
Java Build Path
I don't know why?
Appreciation
Any replies or tips about this issue will be appreciated.
What did I try?
First, I have checked
the two code are same.
can successfully run the Tomcat Server v9.0.
the version of Tomcat Server and its related tools.
Check the project facet of two projects.
Check the url of (two requests sent above) are correct.
I have read these articles.
------ 1st Edit -------
Is the issue caused by?
Different version of Java in project facet
Different the module path
------- 2nd Edit ------------
I have changed version Java in project facet
to 12 of HealthHelper.java
project (which is same as that in HealthHelperServer
).
And I will still get a response code 200. Same as previous result when version Java in project facet is 1.8.
Thus, I wonder that the issue is NOT related to Java in project facet.