Maven'da şöyle
yaparız.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
Kullanım
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface ProductionOrders extends CrudRepository<ProductionOrder, Long> {
}
collectionResourceRel Alanı
Eğer bu repository JSON formatına çevrilirse kullanılacak nesne ismini belirtir.
exported Alanı
false yaparsak SpringData Rest bu repository sınıfı görmezden
gelir.
path Alanı
Rest Endpoint için kullanılacak ismi belirtir.
Örnek
Elimizde şöyle bir kod
olsun.
@RepositoryRestResource(path = "tuple", collectionResourceRel = "tuple")
public interface TupleRepository extends JpaRepository<Tuple, Integer> {
@RestResource(path = "findAllByAnr", rel = "findAllByAnr")
List<Tuple> findAllByAnr(@Param("anr") String anr);
}
Şöyle
yaparız.
http://localhost:8081/tuple/search/findAllByAnr?anr=somevalue
Bulk Insert
SpringData Rest Bulk Insert işlemini
desteklemiyor.
Bidiretional OneToMany İlişki Yaratmak
SpringData Rest bidirectional ilişkileri yönetmiyor. Elimizdeki parent nesne şöyle
olsun
@Data
@Entity
@Table(name = "POTATO")
public class PotatoEntity {
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private BigInteger id;
@Column(name = "FIRSTNAME")
private String firstname;
@Column(name = "LASTNAME")
private String lastname;
@OneToMany(mappedBy = "potato", cascade = CascadeType.PERSIST)
private List<DetailPotatoEntity> detailPotatoList;
}
Many nesne şöyle olsun
@Data
@Entity
@Table(name = "DETAIL_POTATO")
public class DetailPotatoEntity {
@Id
@Column(name = "ID")
private BigInteger id;
@Column(name = "WEIGHT")
private BigDecimal weight;
@Column(name = "HEIGHT")
private BigDecimal height;
@JoinColumn(name = "POTATO_ID", nullable = false)
@ManyToOne
@JsonBackReference(value = "potato-detailPotato")
private PotatoEntity potato;
}
Şu veriyi post edelim
{
"firstname":"patate",
"lastname":"potato",
"detailPotatoList": [
{
"weight":12,
"height":13
}
]
}
İlişkiyi kodda kurmak gerekiyor. Parent sınıfta şöyle
yaparız.
@PrePersist
public void prePersist() {
detailPotatoList.forEach(detailPotato-> detailPotato.setPotato(this));
}
ManyToOne İlişkiye Many Nesne Post Etmek
Örnek
Elimizde şöyle bir kod
olsun. Post nesnesi ilişkinin "One" tarafını temsil eder.
@Entity
public class Post {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
private String title;
@OneToMany
private List<Comment> comments;
// Standard getters and setters...
}
Elimizde şöyle bir kod
olsun. Comment nesnesi ilişkinin "Many" tarafını temsil eder.
@Entity
public class Comment {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
@ManyToOne
private Post post;
// Standard getters and setters...
}
Many tarafa yeni bir Comment eklemek için şöyle
yaparız.
http://{server:port}/comment METHOD:POST
{"author":"abc","content":"PQROHSFHFSHOFSHOSF", "post":"http://{server:port}/post/1"}
Aynı şeyi farklı bir şekilde de çözebiliriz. Önce bir comment nesnesi yaratmak için şöyle
yaparız.
curl -X POST -H "Content-Type: application/json" $url
{ … // your payload // … }
201 Created
Location: $comment_url
Daha sonra bu sefer PUT ile Comment nesnesi Post nesnesine eklenir. Şöyle
yaparız. Birden fazla Comment nesnesini Post nesnesine tek bir PUT ile ekleme imkanı da var.
curl -X PUT -H "Content-Type: text/uri-list" $association_url
$comment_url
204 No Content
Örnek
Elimizde şöyle bir kod
olsun. Container nesnesi ilişkinin "One" tarafını temsil eder.
@javax.persistence.Entity
@Table(name = "CONTAINER")
public class Container implements Serializable {
...
@OneToMany(fetch = FetchType.LAZY, mappedBy = "container", cascade = CascadeType.ALL)
private List<Item> items;
}
Elimizde şöyle bir kod
olsun. Record nesnesi ilişkinin "Many" tarafını temsil eder.
@javax.persistence.Entity
@Table(name = "ITEM")
public class Record implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Basic
private Long value;
@Basic
private Date date;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CONTAINER_ID")
private Container container;
}
Many tarafa yeni bir Record eklemek için şöyle
yaparız.
POST { "value" : 666, "date" : "2016-01-31T23:00:00.000+0000",
"container":"http://localhost:8080/container/1"}