📘

Swagger in Spring Boot Application

Jul 16, 2024

Lecture on Swagger in Spring Boot Application

Overview

  • Two-part video series:
    • Part 1: Introduction to Swagger & its importance
    • Part 2: Integrating Swagger into Spring Boot application and generating API documentation

What is Swagger?

  • Swagger: A set of open-source tools for API development
    • Helps in developing, documenting APIs, and making calls to them
    • Very popular for API documentation through Swagger UI
    • Often misunderstood as just Swagger UI, but it's more comprehensive

Why Use Swagger for API Documentation?

  • Problem: Manual documentation of APIs is cumbersome and often outdated
  • Solution: Swagger automates the documentation process
    • Adds metadata to the code to create auto-generated documentation
    • Updates documentation automatically when APIs change

Benefits of Using Swagger

  • Automation: Reduces manual effort in documenting and updating APIs
  • Consistency: Ensures documentation is always up-to-date
  • Standardization: Provides a standardized way to document APIs

Steps for Integrating Swagger in a Spring Boot Application

Step 1: Add Swagger Dependency

  • Specification for Swagger: Requires spring fox swagger-2 dependency
  • Dependency added to pom.xml: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>

Step 2: Enable Swagger

  • Annotation: Use @EnableSwagger2 annotation in a configuration class
  • Example: @SpringBootApplication @EnableSwagger2 public class SpringBootSwagger2Application { public static void main(String[] args) { SpringApplication.run(SpringBootSwagger2Application.class, args); } }

Step 3: Create API Endpoints

  • Example API: An address book resource with endpoints for contacts
  • Example methods:
    • getAllContacts(): Returns a list of contacts
    • getContact(id): Returns a specific contact
    • addContact(contact): Adds a contact to the list

Step 4: Test API with Postman

  • Postman: A tool to make API calls and test endpoints
  • Verifying API functionalities:
    • GET request: Fetch all contacts or a particular contact
    • POST request: Add a new contact

Generating Documentation

JSON Documentation

  • Swagger provides an endpoint: localhost:8080/v2/api-docs generating JSON documentation
  • Contains details: Paths, responses, models, etc.

HTML Documentation (Swagger UI)

  • Additional Dependency: springfox-swagger-ui <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
  • Accessing Swagger UI: Visit localhost:8080/swagger-ui.html
  • Features:
    • Interactive API documentation
    • Allows direct API testing
    • Provides a user-friendly interface

Customizing Swagger Documentation

  • Customization Needs: Better descriptions, cleaner documentation without default endpoints
  • Annotations for Customization:
    • Descriptions, Notes, API names, etc.
  • To Be Covered in Next Video: Detailed customization for better clarity and usability of the documentation.