Create RESTful Web Service with JAX-RS and deploy it to tomcat

java, rest, tomcat, web-services

Solution

You have the wrong class for your Servlet. Not sure why you are not wanting to use an IDE, but there is a maven archetype that will layout your project structure for you using the appropriate classes that the Jersey developers have defined. My web.xml looks like this:

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.pluralsight</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

I cover all of this in this course here.

Problem

I'm trying to create and deploy a RESTful Web Service using JAX-RS and deploy it to tomcat. I don't want to use any IDE. In Tomcat I have the following directory structure inside webapps\ ``` notifire\WEB-INF\ | ---> web.xml | ---> \classes/Notifier.class | ---> \lib\javax.ws.rs-api-2.0 ``` my web.xml contains: ``` <servlet> <servlet-name>Web Service Servlet</servlet-name> <servlet-class>javax.ws.rs.core.Application</servlet-class> </servlet> <servlet-mapping> <servlet-name>Web Service Servlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> ``` and the class file `Notifier.class` was compiled from the file `Notifier.java`. ``` import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; @Path("notifier") public class Notifier { @Context private UriInfo context; @GET @Produces("text/html") public String getHTML() { return "<p></p>"; } } ``` When I try to access the Web Service at `http://localhost:8080/notifire/webservice/notifier` I get the following error: --type Exception report --message Class javax.ws.rs.core.Application is not a Servlet --description The server encountered an internal error that prevented it from fulfilling this request. Any help is appreciated.

Original source

Related problems