What follows are what I hope are relatively complete solutions to your questions.
You don't mention Maven, so I will: Maven is your friend here.
Let's start with a pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.groupid</groupId>
<artifactId>stack</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
<build>
<finalName>stack</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<name>Stack</name>
</project>
That might not be the absolute minimum in terms of dependencies, but it's close.
But that's just the pom. The trickery continues in the web.xml and the Java classes.
About that web.xml...
It's insanely complicated, so bear with me:
<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"
metadata-complete="false"
version="3.1">
</web-app>
Ok, maybe not that complicated.
Note that setting metadata-complete="true"
may result in Tomcat starting faster.
A pair of Java classes
One is the "application", the other is the rest call.***
The rest call is pretty straightforward:
package some.package;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/hello")
public class HelloRest {
@GET
public String message() {
return "Hello, rest!";
}
}
The application looks like this:
package some.package;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import some.package.HelloRest;
@ApplicationPath("/rest")
public class RestApp extends Application {
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(HelloRest.class));
}
}
And that's it. When you navigate to something like http://localhost:8080/stack/rest/hello
, you should see the text "Hello, rest!"
Leverage Jersey a bit.
getClasses()
in RestApp is a little ugly. You might use Jersey's ResourceConfig, as at the Jersey User's Guide, which would look like this:
public class RestApp extends ResourceConfig {
public RestApp() {
packages("some.package");
}
}
But I don't want to use Maven!
Fine. These are the jars Eclipse lists as Maven dependencies:
- javax.servlet-api-3.1.0.jar
- jersey-container-servlet-core-2.13.jar
- javax.inject-2.3.0-b10.jar
- jersey-common-2.13.jar
- javax.annotation-api-1.2.jar
- jersey-guava-2.13.jar
- hk2-api-2.3.0-b10.jar
- hk2-utils-2.3.0-b10.jar
- aopalliance-repackaged-2.3.0-b10.jar
- hk2-locator-2.3.0-b10.jar
- javassist-3.18.1-GA.jar
- osgi-resource-locator-1.0.1.jar
- jersey-server-2.13.jar
- jersey-client-2.13.jar
- validation-api-1.1.0.Final.jar
- javax.ws.rs-api-2.0.1.jar
- jersey-container-servlet-2.13.jar
Presumably, adding those manually to your classpath should work. Or use Maven.