In our REST APIs, we want to use path-based versioning for REST endpoints. Let's say we have a /v1/students GET endpoint. We have a corresponding controller method like:
@GetMapping("/v1/students")
public List<Student> getStudents(){
return studentService.getStudents();
}
Inside a controller class called SchoolControllerV1.java (along with 20 different other endpoints.) And months later we have a breaking change in response structure and we need a new version like /v2/students GET.
@GetMapping("/v2/students")
public List<StudentV2> getStudents(){
return studentService.getStudentsV2();
}
Inside a controller class called SchoolControllerV2.java (along with 20 different other endpoints.) The problem is the other 19 controller methods which are exactly identical in V1 and V2 controllers are duplicated.
What is the best way of having multiple versions of controllers on a Spring Boot application?