0

I just started JAX-RS (Jersey) for my RESTful web API. Tomcat runs normally but root is not found (error 404).

The error console is as below:

SEVERE: Servlet [Jersey REST Service] in web application [/com.G8.ws] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1858)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1709)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5253)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5543)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

My simple code is as below, as my package name is com.G8.ws, I made http://localhost:8080/com.G8.ws

package com.G8.ws;


import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/v1/status")
public class V1_status {

    @GET
    @Produces(MediaType.TEXT_HTML)
    public String returnTitle(){
        return "<p>RESTful Web API using JAX/RS</p>";
    }

}

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://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>com.G8.ws</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
  <!--   <param-name>com.sun.jersey.config.property.packages</param-name>-->
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>com.G8.ws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>

</web-app>

And, I have downloaded all jar files in jersey-archive-1.19

What could be wrong?

jsh6303
  • 2,010
  • 3
  • 24
  • 50

1 Answers1

1

You need to 2.x bundle or change the ServletContainer declared to the 1.x version.

<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

If you change to the 1.x version, then you would use the commented out package scanning property instead

<param-name>com.sun.jersey.config.property.packages</param-name>
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I see. So uncomment the `com.sun.jersey.config.property.packages` ? – jsh6303 Oct 05 '15 at 05:11
  • Just change the namespace to the one above. `org.glassfish` is for Jersey 2.x, and so is the package scanning property you are using. The one commented out is for 1.x – Paul Samsotha Oct 05 '15 at 05:11
  • Do I need to remove `org.glassfish.jersey.servlet.ServletContainer`? – jsh6303 Oct 05 '15 at 05:15
  • You still need the `` declaration, but change the namespace of the `ServletContainer`. You are getting a `ClassNotFoundException` because the class in your above comment does not exist in 1.x archive (it exists in the 2.x archive). You need to change it to the one in the 1.x archive, which I have in my answer. – Paul Samsotha Oct 05 '15 at 05:17
  • Also do I need to remove the `jersey.config.server.provider.packages` in order to keep only the param-name? – jsh6303 Oct 05 '15 at 05:18
  • replace it with the one you commented out – Paul Samsotha Oct 05 '15 at 05:18
  • So `org.glassfish.jersey.servlet.ServletContainer` should be `org.glassfish.jersey.servlet`? – jsh6303 Oct 05 '15 at 05:21
  • Please re-read my answer and review my comments. You need the `ServletContainer` but change the namespace (package name) – Paul Samsotha Oct 05 '15 at 05:23
  • Thanks for your advice. I made the change but the same error still exists, except for there is not the "SEVERE" error in the console. – jsh6303 Oct 05 '15 at 05:29
  • I am suspecting `Oct 05, 2015 1:31:42 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Root resource classes found: class com.G8.ws.V1_status Oct 05, 2015 1:31:42 AM com.sun.jersey.api.core.ScanningResourceConfig init INFO: No provider classes found.` is related to the 404 error. – jsh6303 Oct 05 '15 at 05:35
  • It not related to 404. See the first part of the `INFO`. It says the resource class `V1_status` is loaded. If you are getting a 404, then you are trying to access the wrong URL. You should be trying to access `//api/v1/status` – Paul Samsotha Oct 05 '15 at 05:37
  • Yes you are right! I think the corrected URL worked. Thank you! – jsh6303 Oct 05 '15 at 05:38