3

I am new to web services. So I started with a small program like the following.

It's working perfectly in GlassFish server but not in Tomcat (I want to run on Tomcat). It's a simple program which gives only the idea of how to run the web service application.

FirstRestService.java:

package com.sandy.demo;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/resources")
public class FirstRestService extends Application {

}

Employees.java:

package com.sandy.demo;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/employees")
public class Employees {

    @GET
    public String getEmployeesNames() {
        return "Hello World";
    }
}

I have included the jsr311-api-1.1.1.jar (JAX-RS API JAR file).

I took a working WAR file of the application and deployed in GlassFish Server. Then I ran the server with the URL: http://localhost:8080/MyFirstRestApplication/resources/employees.

But I am unable to do the same in Tomcat.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
sandy
  • 93
  • 2
  • 9

1 Answers1

6

Please note that jsr311-api-1.1.1.jar refers to JAX-RS 1.1. GlassFish Server 4.0 uses JAX-RS 2.0.

Besides the correct JAX-RS dependency JAR, you'll need a JAX-RS implementation such as Jersey (used by GlassFish) or RESTEasy (used by WildFly).

GlassFish already contains all JAX-RS 2.0 and Jersey dependencies you need. Tomcat doesn't.

Since you were using GlassFish, probably you'll choose Jersey. There are two ways to include JAX-RS and Jersey in your Tomcat project:

1. Using Maven

If you are using Maven, add the following dependencies to your pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <!-- if your container implements Servlet API older than 3.0, 
         use "jersey-container-servlet-core"  -->
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.22.1</version>
</dependency>

<!-- Required only when you are using JAX-RS Client -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.22.1</version>
</dependency>

Just ensure you are using the correct artifactId according to your Tomcat Servlet API version: if your Tomcat implements Servlet API older than 3.0, use jersey-container-servlet-core. Otherwise, use jersey-container-servlet.

On Tomcat documentation you'll find information about the Servlet API version used by each Tomcat version. To summarize:

  • Tomcat 8.0.x: Servlet API 3.1
  • Tomcat 7.0.x: Servlet API 3.0
  • Tomcat 6.0.x: Servlet API 2.5

The latest version of each artifactId can be checked in the Maven Repository:

See more information about Jersey dependencies on the documentation page.

2. Manually downloading JAX-RS and Jersey dependencies

If you are not using Maven, refer to Jersey download page, download the Jersey JAX-RS 2.0 RI bundle and include the dependencies on the classpath.

The Jersey JAX-RS 2.0 RI bundle contains the JAX-RS 2.0 API JAR, all the core Jersey module JARs as well as all the required 3rd-party dependencies.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359