Arşivler

HTTP Service Client Nedir – Spring Boot 4.0

HTTP Service Client Nedir – Spring Boot 4.0 görseli
HTTP Service Client- Spring Boot 4.0

Spring Boot Dersleri‘ne devam ediyorum.

Bu yazıda Spring Boot 4.0 ile gelen Http Service Clients’a yeni özelliklerinden HttpExchange arayüzünden ( @GetExchange @PostExchange ) bahsedeceğim.

Http Service Client Nedir?

Bu yeni yaklaşım, geliştiricilerin bir REST istemcisini @HttpExchange gibi anotasyonlar kullanarak Java interface’leri üzerinden tanımlamasını sağlamaktadır. Bu özellik sayesinde tekrarlayan kalıp kod miktarı ciddi ölçüde azalmaktadır. @ImportHttpServices anotasyonu sayesinde oluşturduğumuz interface’lerin Spring Bean olarak ele alınması sağlanmaktadır.

Anotasyonlar kullanarak oluşturduğumuz interface’lerdeki metotların nasıl bir davranış sergilemesi gerektiğini belirleyebiliyoruz:

  • @HttpExchange
  • @GetExchange
  • @PostExchange
  • @PutExchange
  • @DeleteExchange

Uygulama Örneği

blank

Yapacağımız örnek uygulamada Pokémon API’sine istek atarak erişim sağlayacağız.

Record sınıfımızı oluşturalım.

public record Pokemon(Long id,
                      String name,
                      int height,
                      int weight
                    ) {

}
Code language: Java (java)

@HttpExchange ile işaretleyeceğimiz interface’imizi oluşturalım ve nasıl bir davranış sergilemesi gerektiğini belirtelim.

@HttpExchange(url = "https://pokeapi.co/api/v2/pokemon", accept = "application/json")
public interface PokemonService {

  @GetExchange("/{id}")
  Pokemon findById(@PathVariable Integer id);

  @GetExchange
  List<Pokemon> findAll();

  @PostExchange
  Pokemon create(@RequestBody Pokemon pokemon);

  @PutExchange("/{id}")
  Pokemon update(@PathVariable Integer id, @RequestBody Pokemon pokemon);

  @DeleteExchange("/{id}")
  void delete(@PathVariable Integer id);
}
Code language: Java (java)

@HttpExchange: Spring’in HTTP istemci anotasyonudur. Temel API endpoint’ini ve genel ayarları tanımlar.

Devamında CRUD işlemleri için tanımlayabileceğimiz metotlarımız bulunmaktadır.

Controller’ımızı yazabiliriz.

@RestController
@RequestMapping("/api/pokemon")
public class PokemonController {

  private final PokemonService pokemonService;

  public PokemonController(PokemonService pokemonService) {
    this.pokemonService = pokemonService;
  }

  @GetMapping
  public List<Pokemon> findAll() {
    return pokemonService.findAll();
  }

  @GetMapping("/{id}")
  public Pokemon findById(@PathVariable Integer id) {
    return pokemonService.findById(id);
  }

  @PostMapping
  public Pokemon create(@RequestBody Pokemon pokemon) {
    return pokemonService.create(pokemon);
  }

  @PutMapping("/{id}")
  public Pokemon update(@PathVariable Integer id, @RequestBody Pokemon pokemon) {
    return pokemonService.update(id, pokemon);
  }

  @DeleteMapping("/{id}")
  public void delete(@PathVariable Integer id) {
    pokemonService.delete(id);
  }
}
Code language: Java (java)

Uygulamamızda HttpExchange HTTP Interface’leri, Spring context’ine otomatik olarak bean olarak kaydetmek için kullanılır. Bu sayede manuel bean tanımlamasına gerek kalmaz.

@SpringBootApplication
@ImportHttpServices(types = {PokemonService.class})
public class PokemonappApplication {

  public static void main(String[] args) {
    SpringApplication.run(PokemonappApplication.class, args);
  }
}Code language: JavaScript (javascript)

Uygulama Örneğini İndir

Projenin Kaynak Kodlarına Ulaş


Önceki Ders: TestContainers ile Spring Boot & MongoDB Test İşlemleri
Spring Boot Dersleri
Sonraki Ders:

İlgili Spring içerikleri

İlgili Spring ve DevOps içerikleri

Spring Boot dersleri, Spring Cloud Bus, Testcontainers ve Docker notları ile ilgili rehberleri inceleyin.

İlgili Spring Boot rehberleri

HTTP istemcilerini kullanırken Spring Boot Dersleri rehberini ve Spring Boot uygulamasını Google Cloud App Engine’e dağıtma örneğini inceleyebilirsiniz.