23 Eylül 2020 Çarşamba

SpringData Rest Kullanımı

Giriş
Tüm @RepositoryRestResource olarak işaretli repository sınıflarını tarar ve HATEOAS formatında çıktı verir. Açıklaması şöyle. @RepositoryRestResource Anotasyonu yazısına bakabilirsiniz.
By default, Spring Data REST will export ALL top-level, public interface repositories. You only need @RepositoryRestResource to either NOT export an interface, or to alter the details of the endpoint.
Enpoint'lerin ismi repository arayüzünün çoğul hale getirilmiş halidir.

Maven
Şu satırı dahil ederiz
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency>

1. Resource Discoverability İçin Kullanılacak URL
Örneği application.properties dosyası şöyle olsun
spring.data.rest.base-path=myapi
Bu durumda 
"localhost:8080/myapi" adresine gittiğimizde SpringData Rest tarafından bilinen rest endpoint'leri görebiliriz. Eğer base-path tanımlı değilse "http://localhost:8080/" adresini kullanırız

Örnek
Şöyle yaparız
spring.data.rest.base-path=/api

2. Ne Zaman Spring Data Rest yerine Custom Service Kullanmalı?
Ne zaman Custom Service yazmalı ve ne zaman Spring Data Rest kullanmalı sorusu hep kafa karıştırıyor. Bu sorunun cevaplarından birisi şöyle. Eğer sadece basit CRUD benzeri işler REST üzerinden yapacaksak Spring Data Rest yeterli. Açıklaması şöyle
Spring Data is an additional convenient mechanism for interacting with database entities, organizing them in a repository, extracting data, changing it. in some cases, it will be enough to declare the interface and method in it, without implementing it. P.S and it's a good choice if you're creating a simple crud
3. Repository İçin URL Örnekleri
- findAll işlemi için repository ismi yazılır.
findAll işlemi yazısına taşıdım

- findById, Delete, Post , Put işlemleri için Repository'den sonra id yazılır. Şöyle yaparız.
http://localhost:8080/people/1

- http://localhost:8080/myendpoint/search şeklinde yazarsak repository sınıfındaki custom Query'ler gösterilir.

- http://localhost:8080/people/search/findByLastName?name=Baggins şeklinde yazarsak findByLastName metodunu parametreler ile çağırırız.

3. URI Template Kullanımı
- Eğer sayfalama istemiyorsak Repository nesnemizi JpaRepository, PaginsAndSortingRepository yerine CrudRepository 'den kalıtmak gerekir.
- Veya spring.data.rest.default-page-size değeri değiştirilebilir. Varsayılan sayfa büyüklüğü 20

Page ve Size Parametreleri
- ?size=10 yazarsak sayfa büyüklüğü değiştirilir.
- ?page=1 yazarsak kaçıncı sayfayı isteğimizi belirtiriz.

Sort Parametresi
?sort= şeklinde yaparız.

Projection 
Parametresi
Örnek ver

4. İlişki Kuralları
Açıklaması şöyle.
1) If you do not define a Repository for the child type, a link will not be generated, and it will instead be embedded in the parent object. Of course, this only is useful if you don't need a Repository for that object.

2) FetchType.EAGER will eagerly load the child object. Since the object is already loaded, there is no reason to provide a link to it, so spring-data-rest will embed the object. Make sure to put this on the side with the single object, not the Collection

Hiç yorum yok:

Yorum Gönder