BackEnd/Spring Boot

버전 관리(Versioning)

hanseom 2022. 4. 26. 08:35
반응형

Overview

  해당 글에서는 REST API 버전 관리 방법에 대해 알아보겠습니다.

 

URI Versioning

  모든 URI에 공통적으로 적용되는 "/goods"의 경우 @RequestMapping 애노테이션을 사용하여 설정합니다.

@RestController
@RequestMapping("/goods")
public class GoodsController {

  @GetMapping(path = "/v1/{goodsCode}") // endpoint
  public Goods findGoodsV1(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }

  @GetMapping(path = "/v2/{goodsCode}") // endpoint
  public Goods findGoodsV2(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }
}

 

Request Parameter Versioning

@RestController
@RequestMapping("/goods")
public class GoodsController {

  @GetMapping(path = "/{goodsCode}/", params = "version=1")
  public Goods findGoodsV1(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }

  @GetMapping(path = "/{goodsCode}/", params = "version=2")
  public Goods findGoodsV2(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }
}

 

Headers Versioning

@RestController
@RequestMapping("/goods")
public class GoodsController {

  @GetMapping(path = "/{goodsCode}", headers = "X-API-VERSION=1")
  public Goods findGoodsV1(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }

  @GetMapping(path = "/{goodsCode}", headers = "X-API-VERSION=2")
  public Goods findGoodsV2(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }
}

 

Media type Versioning

@RestController
@RequestMapping("/goods")
public class GoodsController {

  @GetMapping(path = "/{goodsCode}", produces = "application/vnd.company.appv1+json")
  public Goods findGoodsV1(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }

  @GetMapping(path = "/{goodsCode}", produces = "application/vnd.company.appv2+json")
  public Goods findGoodsV2(@PathVariable int goodsCode) throws Exception {
  	/* ... 생략 ... */
  }
}

반응형