I have Knowledge of the Web container and Tomcat and can deploy static and dynamic web sites. But I am new to REST and Jersey. I have read the 2.6 user's guide, reviewed many sites and youtube videos. There seems to be a lot of info on 1.x Jersey but not much on 2.x I can get 1.18 working in my environment but can't seem to get any deployment models working for 2.x. I noticed in 2.x there is an Application deployment model. So I thought i would ask some very generic questions to get this started.
Thanks
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.
<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.
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.
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!"
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");
}
}
Fine. These are the jars Eclipse lists as Maven dependencies:
Presumably, adding those manually to your classpath should work. Or use Maven.