Showing the not found 404. Path is correct i think and no errors are showing. Let me help with this.
.java file
@Path("/foods")
public class FoodService {
List<Food> foods;
ArrayList<Food> foodCart = new ArrayList<>();
public FoodService() {
foods = FoodController.getFoodList();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Food> getFoodList() {
return foods;
}
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Food Service</display-name>
<servlet>
<servlet-name>food_service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>foodservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>food_service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
the link i tried to get value : http://localhost:8080/food_service/rest/foods
Can you try by replacing the display-name and param-value tag values with the package name where class FoodService is located.
Please refer the link in case of confusions.
Since you're using Servlet Spec 3.1 you're on a fairly modern version of Glassfish. In that case you're making your life way too hard. I would recommend either removing your web.xml
or change it to be basically empty:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
Then, in any package, add the code below. This tells the container (Glassfish) that you're running a REST application and serves a similar function as the url-pattern
that you have in your current web.xml
:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/rest")
public class RestApplication extends Application {
// intentionally empty
}
With that, you will call your service with http://localhost:8080/<webapp name>/rest/foods
where webapp name
is the name of your web application.