Spring Framework 7 API 버전 관리
중복된 버전 접두사 컨트롤러를 Spring Framework 7의 네이티브 API 버전 관리로 대체합니다.
코드 비교
✕ Spring Boot 2/3
// Version 1 controller
@RestController
@RequestMapping("/api/v1/products")
public class ProductControllerV1 {
@GetMapping("/{id}")
public ProductDtoV1 getProduct(
@PathVariable Long id) {
return service.getV1(id);
}
}
// Version 2 — duplicated structure
@RestController
@RequestMapping("/api/v2/products")
public class ProductControllerV2 {
@GetMapping("/{id}")
public ProductDtoV2 getProduct(
@PathVariable Long id) {
return service.getV2(id);
}
}
✓ Spring Framework 7+
// Configure versioning once
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(
ApiVersionConfigurer config) {
config.useRequestHeader("X-API-Version");
}
}
// Single controller, version per method
@RestController
@RequestMapping("/api/products")
public class ProductController {
@GetMapping(value = "/{id}", version = "1")
public ProductDtoV1 getV1(@PathVariable Long id) {
return service.getV1(id);
}
@GetMapping(value = "/{id}", version = "2")
public ProductDtoV2 getV2(@PathVariable Long id) {
return service.getV2(id);
}
}
이 코드에 문제가 있나요? 알려주세요.
모던 방식이 더 나은 이유
컨트롤러 중복 없음
모든 버전이 하나의 컨트롤러 클래스에 있습니다; 개별 핸들러 메서드만 버전 속성을 가집니다.
중앙화된 버전 전략
단일 configureApiVersioning 호출로 헤더에서 URL이나 쿼리 파라미터 버전 관리로 전환합니다.
점진적 진화
관련 없는 엔드포인트나 새 컨트롤러 파일을 건드리지 않고 하나의 메서드에 새 버전을 추가합니다.
이전 방식
수동 URL 경로 버전 관리
모던 방식
네이티브 API 버전 관리
JDK 버전
17
난이도
중급
JDK 지원
Spring Framework 7 API 버전 관리
사용 가능
Spring Framework 7.0 (Java 17+ 필요) 이후 사용 가능
동작 원리
Spring Framework 7 이전에는 API 버전 관리를 위해 별도의 컨트롤러 클래스나 복잡한 수동 라우팅이 필요했습니다. Spring 7의 네이티브 API 버전 관리는 이를 단순화합니다.
관련 문서