In setting up a Jetty server with Jersey servlets, you may choose to use an embedded Jetty server setup. (See here). In this gist, we'll go through how to setup Swagger for this setup.
In the Swagger Core setup, the only recommendations are involving a regular Application class, or a web.xml, neither of which are used in an embedded Jetty server setup. To add Swagger to your embedded Jetty Server, you must do 3 things:
- Add the package scanning packages to your servlets which will serve your REST API.
- Add the Swagger package scanning servlet.
- [Optional] Package the Swagger-UI static HTML5 content with your server
Let's start with a basic server class, adapted from the Eclipse tutorial above.
public class OneServletContext { public static void main(String[] args) throws Exception { Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
ServletHolder apiServlet = context.addServlet(ServletContainer.class, "/api/*");
apiServlet.setInitOrder(1);
apiServlet.setInitParameter("com.sun.jersey.config.property.packages", "com.api.resources");
server.start();
server.join();
}
}