Question

I am currently implementing my first JAX-RS based REST service and during my research on JAX-RS I found two version how to implement the main 'entry point' of the application. One option is to implement a class that starts a server in its main method:

public class Spozz {
    public static void main(String[] args) throws Exception {
        //String webappDirLocation = "src/main/webapp/";
        int port = Integer.parseInt(System.getProperty("port", "8087"));
        Server server = new Server(port);

        ProtectionDomain domain = Spozz.class
                .getProtectionDomain();
        URL location = domain.getCodeSource().getLocation();

        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
        webapp.setResourceBase(location.toExternalForm());
        webapp.setServer(server);
        webapp.setWar(location.toExternalForm());

        webapp.setParentLoaderPriority(true);

        // (Optional) Set the directory the war will extract to.
        // If not set, java.io.tmpdir will be used, which can cause problems
        // if the temp directory gets cleaned periodically.
        // Your build scripts should remove this directory between deployments
        webapp.setTempDirectory(new File(location.toExternalForm()));

        server.setHandler(webapp);
        server.start();
        server.join();
    }
}

The other one to extend the javax.ws.rs.core.Application class.

@ApplicationPath("/services")
public class Spozz extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> empty = new HashSet<Class<?>>();

    public Spozz() {
        singletons.add(new UserResource());
    }

    @Override
    public Set<Class<?>> getClasses() {
        return empty;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

However, I have seen both version in many different examples, but it was never explained why this version was chosen and what the benefits are. I personally would stick with the second one, but I do not really have a reason for that. So my questions are:

  1. What are the differences of this options?
  2. Which one is preferable when using JAX-RS 2.0?
  3. Is it important if I would like to add servlets to the service?
Was it helpful?

Solution

What are the differences of this options?

Option #1 is really only meant as a quick start. There are many attributes to a web container that you'd want to tweak in a production environment, such as thread pools / number of threads / connectors / etc. I'm unsure about which embedded container your code relates to, but web container configuration is best left to config files and not code in production.

Which one is preferable when using JAX-RS 2.0?

If you're prototyping, use #1. For production use #2. For an entry point into a jersey application, extending the Application class is the way to go. So it is not really a matter of choice.

Is it important if I would like to add servlets to the service?

I'm not sure what that means. You'd like to add servlets at a later point in time ? It does not matter if you're using an embedded container or a standalone container. Servlets are servlets. They'll serve requests when they are in a container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top