Categories: Uncategorised

Spring Boot 3 REST API Doc + Swagger

Springdoc-openapi library helps to automote the generation of REST API documentation .

For Spring boot V3 we need to use springdoc-openapi V2 . 

For integration between spring-boot and swagger-ui add the below dependency . Then the swagger will be available  at http://server:port/context-path/swagger-ui.html

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

Sample API:

EmployeeController

@RestController
@RequestMapping("/api/v1/")
@Tag(name = "Employee", description = "the Employee API")
public class EmployeeController {

 @Autowired
 private EmployeeService svc;
 
 @GetMapping("/employees")
 public List<Employee> getAllEmployees() {
  return svc.getall();
  
 }
 
 @Operation(summary = "To add an employee", tags = { "Employee" })
 @ApiResponses(value = { @ApiResponse(description = "successful operation", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Employee.class)), @Content(mediaType = "application/xml", schema = @Schema(implementation = Employee.class)) }) })
 @PostMapping(value="employee/add",consumes = { "application/json" })
 public Employee addEmp(@RequestBody Employee emp) {
  return svc.add(emp);
  
   
 }
}

Employee Model

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@JacksonXmlRootElement(localName = "employee")
@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
 
 @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
 @JsonProperty("id")
 @JacksonXmlProperty(localName = "id")
    private Long employeeId;
 
 @JsonProperty("name")
 @JacksonXmlProperty(localName = "name")
 String name;
 
 @JsonProperty("department")
 @JacksonXmlProperty(localName = "department")
 String department;

}

After running the application we can view the swagger using below url

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

References:

https://springdoc.org/v2/

Github URL :

https://github.com/MMahendravarman/Springboot_Examples
mahendravarman.m@gmail.com

Recent Posts

Spring Webflux Functional Endpoint – File Upload

In this blog using the Spring WebFlux module, we are going to leverage the functional…

9 months ago

Serverless Functions with Spring Cloud Function, AWS Lambda

Spring Cloud Function is a project within the Spring ecosystem that allows developers to build…

9 months ago

Spring Boot + RabbitMQ – Decoupling Microservices Communication

RabbitMQ is an open-source message broker software that implements the Advanced Message Queuing Protocol (AMQP).…

9 months ago

Spring Integration – Sending files over SFTP

Spring Integration is a powerful extension of the Spring Framework designed to support the implementation…

9 months ago

Spring Cloud Config Client

The Spring Cloud Config Client is a component of the Spring Cloud framework that enables…

10 months ago

Handling CSV in Python

In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.…

10 months ago