20 Haziran 2023 Salı

API Versioning

Giriş
Yöntemler şöyle
URL'yi Değiştiren Yöntemler
1. URL Path Versioning
2.  Request Parameter Versioning

URL'yi Değiştirmeyen Yöntemler
1. Media Type veya Content Negotiation (Accept Header) Versioning

1. URI/URL Path Versioning
Açıklaması şöyle
URL path versioning involves including the version number directly in the URL path of the API endpoints. This is a common approach and makes the version explicit in the URL.
Örnek
Şöyle yaparız
@RestController
@RequestMapping("/api/v1/users")
public class UserControllerV1 {

  @GetMapping
  public ResponseEntity<List<User>> getUsers() {
    // Implementation here
  }
  // Other endpoints
}
2. Request Parameter Versioning
Açıklaması şöyle
In this approach, the version number is included as a query parameter in the URL.
Örnek
Şöyle yaparız
@RestController
public class UserController {

  @GetMapping(value = "/api/users", params = "version=1")
  public ResponseEntity<List<User>> getUsersV1() {
    // Implementation here
  }

  @GetMapping(value = "/api/users", params = "version=2")
  public ResponseEntity<List<User>> getUsersV2() {
     // Implementation here
  }
}
URL'yi Değiştirmeyen Yöntemler
1.Custom Header Versioning
2. Content Negotiation (Accept Header) Versioning

1.Custom Header Versioning
Örnek
Şöyle yaparız
@RestController
public class UserController {

  @GetMapping(value = "/api/users", headers = "X-API-Version=1")
  public ResponseEntity<List<User>> getUsersV1() {
    // Implementation here
  }

  @GetMapping(value = "/api/users", headers = "X-API-Version=2")
  public ResponseEntity<List<User>> getUsersV2() {
    // Implementation here
  }
}
2. Content Negotiation veya Media Type (Accept Header) Versioning
Açıklaması şöyle
In this approach, the version is specified in the Accept header of the HTTP request.
Örnek
Şöyle yaparız
@RestController
public class UserController {

  @GetMapping(value = "/api/users", produces = "application/vnd.example.api.v1+json")
  public ResponseEntity<List<User>> getUsersV1() {
    // Implementation here
  }

  @GetMapping(value = "/api/users", produces = "application/vnd.example.api.v2+json")
  public ResponseEntity<List<User>> getUsersV2() {
      // Implementation here
  }
}
Örnek
Açıklaması şöyle
Similar to media type versioning, content negotiation uses the Accept header to determine the version.
Bir başka yöntem şöyle
@Configuration
public class WebConfig implements WebMvcConfigurer {

  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("v1", MediaType.valueOf("application/vnd.company.app-v1+json"));
    configurer.mediaType("v2", MediaType.valueOf("application/vnd.company.app-v2+json"));
  }
}

@RestController
@RequestMapping("/users")
public class UserController {
    
  @GetMapping(produces = "v1")
  public ResponseEntity<String> getUserV1() {
    // Endpoint implementation for version 1
  }
    
  @GetMapping(produces = "v2")
  public ResponseEntity<String> getUserV2() {
    // Endpoint implementation for version 2
  }
}


Hiç yorum yok:

Yorum Gönder