forked from howtoprogram/Java-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookRepositoryImplSpring.java
More file actions
55 lines (38 loc) · 1.43 KB
/
Copy pathBookRepositoryImplSpring.java
File metadata and controls
55 lines (38 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.howtoprogram.repository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.howtoprogram.domain.Book;
public class BookRepositoryImplSpring {
private static final String URI_BOOK = "http://localhost:8080/v1/books";
private RestTemplate restTemplate = new RestTemplate();
public void deleteBook(Long id) {
restTemplate.delete(URI_BOOK + "/{id}", id);
}
public void updateBook(Book book) {
restTemplate.put(URI_BOOK + "/{id}", book, book.getId());
}
public static void main(String[] args) {
BookRepositoryImplSpring repository = new BookRepositoryImplSpring();
// Getting the first book from the RESTful service
Book book = repository.getAllBooks()[0];
// Try to delete the book
repository.deleteBook(book.getId());
}
public Book createBook(Book book) {
// Book createdBook = restTemplate.postForObject(URI_BOOK, book, Book.class);
ResponseEntity<Book> responseEntity = restTemplate.postForEntity(URI_BOOK, book, Book.class);
Book createdBook = null;
if (responseEntity.getStatusCode() == HttpStatus.CREATED) {
createdBook = responseEntity.getBody();
}
return createdBook;
}
public Book[] getAllBooks() {
Book[] books = restTemplate.getForObject(URI_BOOK, Book[].class);
return books;
}
public Book findBookById(Long id) {
return null;
}
}