Forked from nosmokingpistol/Swagger Setup for Embedded Jetty Server.md
Last active
April 21, 2021 04:19
-
-
Save anirtek/08c27e11ae3b0b87f8e4560a38351a48 to your computer and use it in GitHub Desktop.
Setup Swagger with an embedded Jetty Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| In setting up a Jetty server with Jersey servlets, you may choose to use an embedded Jetty server setup. (See https://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Setting_a_ServletContext). 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: | |
| 1. Add the package scanning packages to your servlets which will serve your REST API. | |
| 2. Add the Swagger package scanning servlet. | |
| 3. [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(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment