Issue
Below is my web.xml:
<?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" id="WebApp_ID" version="3.1">
<display-name>testAPI</display-name>
<servlet>
<servlet-name>testAPI</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testAPI</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
My class file looks as below:
package testAPI;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello()
{
String resource="<? xml version='1.0' ?>" + "<hello> hello from XML</hello>";
return resource;
}
/*@GET
@Produces(MediaType.APPLICATION_JSON)
public String sayHelloJSON()
{
String resource=null;
return resource;
}*/
}
My version of java is 8, tomcat 7 and jersey 2.25. I have created a dynamic web project. When i access http://localhost:4080/ - tomcat homepage opens. http://localhost:4080/testAPI/ will show the index.html. However when i try accessing http://localhost:4080/testAPI/rest/hello- i get http status 404 not found
Solution
Your param value in web.xml needs to point to the package containing your class.
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test</param-value>
</init-param>
should be
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>testAPI</param-value>
</init-param>
Answered By - NickMarkoAu