Category: java
REST API Versioning
Published on 24 Jul 2026
Explanation
API versioning allows developers to introduce new features without breaking existing clients. Common versioning strategies include URI versioning, request parameters, custom headers, and media type versioning. This ensures backward compatibility as APIs evolve.
Code:
@RestController
@RequestMapping("/api/v1/students")
public class StudentController {
@GetMapping
public List<Student> getStudents() {
return service.findAll();
}
}
Explanation
HATEOAS (Hypermedia as the Engine of Application State) enhances REST APIs by including hyperlinks in responses. These links guide clients to related resources and available actions, making APIs more discoverable and easier to navigate.
Code:
@GetMapping("/{id}")
public EntityModel<Student> getStudent(@PathVariable Long id) {
Student student = service.findById(id);
return EntityModel.of(student);
}
Explanation
Spring HATEOAS provides the linkTo() and methodOn() methods to create hyperlinks dynamically. These links enable clients to discover related endpoints without hardcoding URLs, improving API usability and maintainability.
Code:
EntityModel<Student> model = EntityModel.of(student);
model.add(linkTo(methodOn(StudentController.class)
.getStudents())
.withRel("all-students"));
Explanation
Swagger (OpenAPI) automatically generates interactive API documentation for Spring Boot applications. Developers can view endpoints, request parameters, response models, and test APIs directly from a web browser without external tools.
Code:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.8</version>
</dependency>
Explanation
OpenAPI annotations such as @Operation and @Parameter describe REST endpoints, making API documentation more informative. After starting the application, Swagger UI provides an interactive interface for exploring and testing every API.
Code:
@Operation(summary = "Get Student By Id")
@GetMapping("/{id}")
public Student getStudent(
@Parameter(description = "Student ID")
@PathVariable Long id) {
return service.findById(id);
}
// http://localhost:8080/swagger-ui/index.html