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

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 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

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 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

    Development

    GPT-5 is Coming: Revolutionizing Software Testing

    July 22, 2025
    Development

    Win the Accessibility Game: Combining AI with Human Judgment

    July 22, 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

    Intel’s new CEO vows to run chipmaker ‘as a startup, on day one’

    News & Updates

    CVE-2025-6826 – Payroll Management System SQL Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Regolith – A JavaScript library immune to ReDoS attacks

    Development

    Joe Biden Strong Cancer Support shirt

    Web Development

    Highlights

    CVE-2025-40672 – ProactivaNet from Grupo Espiral MS Privilege Escalation Vulnerability

    May 26, 2025

    CVE ID : CVE-2025-40672

    Published : May 26, 2025, 10:15 a.m. | 1 hour, 52 minutes ago

    Description : A Privilege Escalation vulnerability has been found in ProactivaNet v3.24.0.0 from Grupo Espiral MS. This vulnerability allows any user to override the file panLoad.exe that will be executed by SYSTEM user via a programmed task.
    This would allow an attacker to obtain administrator permissions to
    perform whatever activities he/she wants, shuch as accessing sensitive
    information, executing code remotely, and even causing a denial of
    service (DoS).

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Identity Security Has an Automation Problem—And It’s Bigger Than You Think

    May 22, 2025

    Rilasciato Fastfetch 2.40: il tool per le informazioni di sistema si aggiorna con nuovi rilevamenti hardware

    April 3, 2025

    Active! Mail RCE flaw exploited in attacks on Japanese orgs

    April 22, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

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