Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Never Stop Exploring (July 2025 Wallpapers Edition)

      June 30, 2025

      How AI further empowers value stream management

      June 27, 2025

      12 Top ReactJS Development Companies in 2025

      June 27, 2025

      Not sure where to go with AI? Here’s your roadmap.

      June 27, 2025

      I never thought I’d praise a kickstand power bank – until I tried this one

      June 30, 2025

      I replaced my work PC with this Alienware laptop – now I’m wondering why I hadn’t done this sooner

      June 30, 2025

      How to set up Alexa to receive notifications on Prime Day deals you want

      June 30, 2025

      How proxy servers actually work, and why they’re so valuable

      June 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      What’s the difference between named functions and arrow functions in JavaScript?

      June 30, 2025
      Recent

      What’s the difference between named functions and arrow functions in JavaScript?

      June 30, 2025

      Spring Boot + Swagger: A Complete Guide to API Documentation

      June 30, 2025

      Wire Room Math: AI + SME = (Less Compensation Paid) X (Headline Risk + Payment Errors)^2

      June 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Artix Linux: Introduzione di XLibre nelle Build Sperimentali

      June 30, 2025
      Recent

      Artix Linux: Introduzione di XLibre nelle Build Sperimentali

      June 30, 2025

      Orange Pi R2S Single Board Computer Running Linux: Introduction

      June 30, 2025

      vmstat – reports virtual memory statistics

      June 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Spring Boot + Swagger: A Complete Guide to API Documentation

    Spring Boot + Swagger: A Complete Guide to API Documentation

    June 30, 2025

    image (38).pngWhen building software, especially APIs (Application Programming Interfaces), clear and accurate documentation is essential. It helps other developers understand how to interact with your service: which routes are available, what data to send, and what kind of response to expect. One of the most widely used tools for API documentation is Swagger.

    As backend developers, we’re deeply familiar with how APIs work. We know which requests are accepted, what responses are returned, what headers are required, which HTTP methods are used (like GET to retrieve data, POST to create, PUT to update, or DELETE to remove resources), and what various status codes mean (200 OK, 404 Not Found, 500 Internal Server Error, etc.).

    But the people using our APIs, such as frontend developers, QA testers, or third-party clients, don’t always have access to that same level of understanding. Unless we clearly document our API, they’re left guessing or constantly asking for clarification. Good documentation saves time, reduces miscommunication, and makes collaboration smoother.

    This is where Swagger comes in. It automatically generates interactive, user-friendly documentation based on the annotations in your code. With Swagger, users can:

    • View available endpoints

    • See required parameters and expected responses

    • Test APIs directly in the browser

    In short, Swagger makes your API easier to understand, easier to test, and easier to adopt—for everyone involved.

    What is Swagger?

    Swagger makes API development easier with free and powerful tools that help you and your team design and document APIs quickly and clearly, even for large projects.

    API Documentation & Design Tools for Teams | Swagger

    This article will walk you through how to add Swagger (which follows the OpenAPI standard) to your Spring Boot application using a popular and easy-to-use library called springdoc-openapi. This tool helps you automatically generate clean and interactive API documentation for your backend.

    https://github.com/ayushstwt/product-backend.git

    I have created a basic CRUD application using Spring Boot. You can clone the project from my GitHub repository.

    git clone https://github.com/ayushstwt/product-backend.git

    Please make sure to switch to the:

    master

    branch to get the base project setup. open the project in your preferred IDE such as IntelliJ IDEA or Eclipse. Make sure Java 17 or higher and Maven are installed on your system. Once everything is set up, you can run the application using the command:

    ./mvnw spring-boot:run

    Step-by-Step: Getting Default OpenAPI Docs and Swagger UI

    Step 1: Add Maven Dependency

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.8.5</version>
    </dependency>

    Step 2: After setting up the above dependency, now we hit the URL from the browser

    http://localhost:8080/v3/api-docs

    and find the OpenAPI descriptions at /v3/api-docs, which is the default path

    image (39).pngThe above image shows a Swagger API documentation for a Spring Boot application. It defines a PUT API at /api/v1/products/update used to update product details. The request must have a JSON body matching the UpdateProductRequest schema. If the update is successful, the API returns a string response with status code 200. The server runs on http://localhost:8080.

    if we want to show the API Data in UI format than we use The springdoc-openapi dependency already includes Swagger UI, so we’re all set to access the API documentation at

    http://localhost:8080/swagger-ui/index.html

    and get the result like

    image (40).pngAll the API’S which are created by us in springBoot application are now by default documented by OpenAPI

    image (41).pngThis image shows the schema for product-related API requests and responses. The UpdateProductRequest includes fields like id, productName, price, description, and imageUrl. These are used to update product details. Other schemas like CreateProductRequest and ProductResponseDTO are also listed.

    image (42).pngThe default OpenAPI configuration is already set up, so we get auto-generated API documentation. This documentation is created by OpenAPI and shows all available endpoints, request and response formats, and schemas in a clear way without writing extra code.

    How to customize the OpenAPI Docs path from application.properties file?

    we can customize the OpenAPI documentation path in a Spring Boot application by updating the application.properties file.

    To change the default path of the OpenAPI JSON docs and Swagger UI, we can add the following properties: springdoc.api-docs.path=/custom-api-docs and springdoc.swagger-ui.path=/custom-swagger-ui. After restarting the application, your OpenAPI docs will be available at http://localhost:8080/custom-api-docs, and the Swagger UI can be accessed at http://localhost:8080/custom-swagger-ui. This is supported in SpringDoc version 2.x and above.

    Open application.properties and Add the following properties

    spring.application.name=product-api
    
    server.port=8080
    spring.datasource.url=jdbc:mysql://localhost:3306/productdb
    spring.datasource.username=root
    spring.datasource.password=ayush@123
    
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true
    
    # Change the OpenAPI JSON docs path
    springdoc.api-docs.path=/custom-api-docs
    
    # Change the Swagger UI path
    springdoc.swagger-ui.path=/custom-swagger-ui

    Why We Use @Tag in Swagger/OpenAPI

    The @Tag annotation is used to group APIs in Swagger UI. It helps organize endpoints by giving them a name and description. For example

    @Tag(
            name = "Product Management",
            description = "APIs for managing products in the system"
    )

    now when we hit the this http://localhost:8080/swagger-ui/index.html endpoint than we get the following changes

    image (43).pngThis means all APIs in this controller will appear under the Product Management section in the Swagger docs. It makes the documentation cleaner and easier to understand, especially in large projects.

    Why We Use @Operation in Swagger/OpenAPI?

    We use @Operation to describe what an API does. It adds a short summary and a clear description to make Swagger docs easy to understand.

    @Operation(
    summary = "Create a new product",
     description = "Add a new product to the system"
     )

    now when we hit the this http://localhost:8080/swagger-ui/index.html endpoint than we get the following changes

    image (44).pngThis helps other developers quickly know the purpose of the API without reading the full code. It makes the documentation clean and useful.

    Why We Use @ApiResponses in Swagger/OpenAPI?

    We use @ApiResponses to show the different responses an API can return. It helps others understand what will happen when they call the API.

    @ApiResponses(value = {
                @ApiResponse(responseCode = "200", description = "Product created successfully",
                        content = @Content(schema = @Schema(implementation = ProductResponseDTO.class))),
                @ApiResponse(responseCode = "400", description = "Invalid request data",
                        content = @Content(schema = @Schema()))
        })

    now when we hit the this http://localhost:8080/swagger-ui/index.html endpoint than we get the following changes

    image (45).pngIt makes the Swagger docs clear and easy to follow.

    Why We Use @ApiResponse in Swagger/OpenAPI?

    We use @ApiResponse to define what a specific HTTP response means for an API. here @Content tells Swagger what the body content of the response (or request) will look like. and @Schema defines the structure of the data, usually by linking to a class (like a DTO).

    @ApiResponse(responseCode = "200", description = "Product created successfully",
                        content = @Content(schema = @Schema(implementation = ProductResponseDTO.class))),
                @ApiResponse(responseCode = "400", description = "Invalid request data",
                        content = @Content(schema = @Schema()))

    Now, when we hit the this http://localhost:8080/swagger-ui/index.html endpoint than we get the following changes

    image (46).pngIt makes the Swagger docs clear and easy to follow request and response that we get and send.

    How to Test API’s in Swagger?

    To test your APIs in Swagger UI, open http://localhost:8080/swagger-ui.html in your browser. You will see a list of available endpoints, such as those under the UserController. Click on an endpoint like POST /api/users, then click “Try it out” to enable input fields. Enter sample data and click “Execute” to send the request. Swagger will show the response body, status code, and headers, allowing you to easily test and debug your APIs.

    You can explore the complete Swagger documentation implementation in this branch:

    GitHub Branch: feat/swagger-doc

    Source: Read MoreÂ

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleWire Room Math: AI + SME = (Less Compensation Paid) X (Headline Risk + Payment Errors)^2
    Next Article What’s the difference between named functions and arrow functions in JavaScript?

    Related Posts

    Artificial Intelligence

    Introducing Gemma 3

    June 30, 2025
    Artificial Intelligence

    Experiment with Gemini 2.0 Flash native image generation

    June 30, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Insights in implementing production-ready solutions with generative AI

    Machine Learning

    Rilasciati Wine 10.8 e GE-Proton 10.1: tutte le novità per GNU/Linux

    Linux

    CVE-2025-49443 – Chris McCoy Bacon Ipsum Cross-site Scripting Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Last Week in AI #312 – Meta’s Superintelligence lab, Anthropic & Midjourney sued

    Artificial Intelligence

    Highlights

    Farmonics Peri Peri Powder – Spicy & Tangy Seasoning for Fries, Grilled Foods & Snacks | Authentic Spice Mix for Cooking & Marination

    May 30, 2025

    Post Content Source: Read MoreÂ

    CVE-2025-20973 – Android Secure Folder Authentication Bypass

    May 7, 2025

    Overwatch 2 gets a ‘Stadium Mode’ gameplay trailer, marking the biggest change the game has seen in years

    April 14, 2025

    Customize URL Handling with Laravel’s Macroable URI Class

    May 13, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.