Paired together with pom.xml gist, this demos automagical loading of REST resources in a standalone servlet container (e.g., Tomcat).
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// App will be hosted at localhost:8080/HelloRESTEasy/rest/(etc)
@ApplicationPath("/rest")
public class ServletHook extends Application {
@Context
ServletContext _servletContext = null;
private Set<Object> _singletons = null;
private static Logger _logger = null;
static {
_logger = LoggerFactory.getLogger(ServletHook.class);
}
public ServletHook() {
_logger.info("Creating ServletHook instance: " + this);
}
@Override
public Set<Object> getSingletons() {
// Lazy initialization.
if (_singletons == null) {
_singletons = new HashSet<Object>();
if (_servletContext == null) {
_logger.warn("Application's reference to servlet context is null.");
}
// Java class handling REST requests. (ServletContext passed for convenience.)
_singletons.add(new HelloWorldService(_servletContext));
}
return _singletons;
}
}