REST with Jersey and Jetty as standalone application

In a previous blog entry I wrote about how you run and setup your Java web application as a standalone application with Jetty and a .war file with maven. In this blog entry I want to show how you can integrate a RESTful interface with Jersey into your Jetty standalone application.


Tell maven first that you need Jetty and the Jersey servlet libraries:

<!-- server/pom.xml -->

Jetty Server

Navigate now to your file and implement the server. We will set the handler that we’ll discuss in the next section.

public static void main( String[] args ) throws Exception {
    Server server = new Server(8080);


Now we should implement a simple handler for dealing with Jersey. At first, we need a context handler, as explained in Java web application as a standalone application with Jetty After setting the context path for the application we will connect Jetty with Jersey through a ServletHolder. In the last step we tell Jersey where to find our Restful classes - that we don’t have yet.

private static Handler getJerseyHandler() {
    ServletContextHandler ctx = 
        new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    ServletHolder servlet = 
        ctx.addServlet(ServletContainer.class, "/api/*");
    return ctx;

RESTful interface is our RESTful interface. It will listen on /ep. The method endpoint1 responds with plain text.

public class Entrypoint {
    public String endpoint1() {
        return "Hallo form endpoint1";


You can now start the server and call http://localhost:8080/api/ep/1 in your browser.

For detailed information about the myjetty project you should visit my github.

Sven Malvik - Lead Software Engineer

Subscribe to my YouTube channel and you will get all my Java and DevOps videos. Hope to see you there!