SpringBoot OpenAPI Docs

springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. SpringBoot3.x version will not support the swagger. If we're working with springboot we need to use the OpenAPI docs.

To implement the OpenAPI docs in a Spring Boot application using Java code, we need to follow these steps:
Step 1: Add Dependencies In your `pom.xml` file add the necessary dependencies.


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

Step2: implement all the required APIs Implement below endpoints:
  1. HTTP POST /api/v1/customer/create – create a customer.
  2. HTTP GET /api/v1/customer/getAll – returns all the customers in the system.
  3. HTTP GET /api/v1/customer/getById/{customerId} – returns a customer by specified id.
  4. HTTP DELETE /api/v1/customer/deleteById/{customerId} – delete a customer by specified id.
  5. HTTP PUT /api/v1/customer/update – update a customer by specified id.
Step3: Run the Application
After implementing the above steps, run your Spring Boot application. By default, documentation will be available at: http://localhost:8095/swagger-ui
You can use this Swagger UI Open API defination to explore and test your APIs interactively. It provides a user-friendly interface where you can see all your API endpoints, their request/response structures, and even test the APIs directly from the browser.


Description to the REST operation

To provide the description to the REST operation we need to Use the @Operation and @ApiResponses annotations.


                    @PostMapping("/create")
                    @Operation(
                            summary = "Registration",
                            description = "Register the customer using the input request object")
                    @ApiResponses(value = {
                            @ApiResponse(responseCode = "200", description = "successful operation")
                    })
                    public ResponseEntity<APIResponse> createCustomer(@RequestBody CustomerRequest request) {
                        return customerService.createCustomer(request);
                    }
                  

Full source code is available in follwong GitHub repository: SpringBoot OpenApi Docs Example