Configure Swagger With Jersey and Spring Boot

Configure Swagger With Jersey and Spring Boot:-

We are going to configure Swagger With Jersey and Spring Boot. We saw applications are moving towards microservices architecture. one of the important aspects of API development is API documentation. Great documentation results in proper experience in API consumption. We are going to use and Configure Swagger and Swagger UI for API documentation along with Jersey and Spring Boot.

Swagger is Used for standardizing API documentation and provide a consistent specification. Swagger UI  helps in visualizing and interact with our API’s.

Integration of Spring with Swagger is a straightforward task with  Spring provided annotations. Using Spring Boot Swagger with JAX-RS is a bit tricky task. Let’s continue and create a  Spring Boot Jersey Project referring our post Spring Boot Jersey Example.

Project Dependencies:-

  1. Spring Boot
  2. swagger-jersey2-jaxrs
  3. spring-boot-starter-web

Here is our pom.xml with spring boot swagger maven dependency and other required dependencies.

Configuration Of Swagger and Jersey:-

Now we have already added swagger maven dependencies, now it’s the time to configure swagger with jersey and spring boot. Lets  add a swagger configuration in our Jersey Config as below.

We are setting basepath asswaggerConfigBean.setBasePath("/api") this path could be anything. On successful setup, we can check our swagger setup is working or not by going to http://localhost:8080/api/swagger.json

Once we get an output JSON as response, our first step to Configure Swagger With Jersey and Spring Boot is Done.

Now the next step is Configuring Swagger UI.

Configure and Setup Swagger UI:-

Visit Swagger UI GitHub project and download dist version of the Project. Extract contents and move the dist content to /src/main/resources/static , we can create the folder structure if it is not present. The dist folder under the GitHub project contains  JavaScript, CSS, images and HTML files used for swagger UI.

We need to update index.htmlinside forsrc/main/resources/static/index.html Swagger UI to find the configuration and load the UI accordingly. Replace text withhttp://petstore.swagger.io/v2/swagger.json to your  swagger.json URL. In this case, our swagger.json is located at http://localhost:8080/api/swagger.json

Testing:-

Swagger Spring Boot and Jersey

Leave a Reply

Your email address will not be published. Required fields are marked *