Skip to content

Commit 01859ff

Browse files
committed
rest client with OkHttp client
1 parent 59b8bec commit 01859ff

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

Java-RESTful-Client-Example/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@
111111
<artifactId>converter-gson</artifactId>
112112
<version>2.1.0</version>
113113
</dependency>
114-
114+
<dependency>
115+
<groupId>com.squareup.okhttp3</groupId>
116+
<artifactId>okhttp</artifactId>
117+
<version>3.4.1</version>
118+
</dependency>
115119
<dependency>
116120
<groupId>junit</groupId>
117121
<artifactId>junit</artifactId>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.howtoprogram.repository.okhttp;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.howtoprogram.domain.Book;
5+
6+
import okhttp3.MediaType;
7+
import okhttp3.OkHttpClient;
8+
import okhttp3.Request;
9+
import okhttp3.RequestBody;
10+
import okhttp3.Response;
11+
12+
public class BookRepositoryImplOkHttp {
13+
public static final MediaType MediaTypeJSON = MediaType.parse("application/json; charset=utf-8");
14+
15+
private static final String URI_BOOK = "http://localhost:8080/v1/books";
16+
17+
public void deleteBook(Long id) throws Exception {
18+
OkHttpClient httpclient = new OkHttpClient();
19+
Request request = new Request.Builder().url(URI_BOOK + "/" + id).delete().build();
20+
try (Response response = httpclient.newCall(request).execute()) {
21+
if (!response.isSuccessful()) {
22+
throw new RuntimeException("Failed to delete book with id:" + id);
23+
24+
}
25+
}
26+
}
27+
28+
public Book updateBook(Book book) throws Exception {
29+
30+
Book updatedBook = null;
31+
OkHttpClient httpclient = new OkHttpClient();
32+
ObjectMapper mapper = new ObjectMapper();
33+
String jsonBook = mapper.writeValueAsString(book);
34+
Request request = new Request.Builder().url(URI_BOOK + "/" + book.getId())
35+
.put(RequestBody.create(MediaTypeJSON, jsonBook)).build();
36+
try (Response response = httpclient.newCall(request).execute()) {
37+
if (response.isSuccessful()) {
38+
updatedBook = mapper.readValue(response.body().bytes(), Book.class);
39+
40+
}
41+
}
42+
return updatedBook;
43+
}
44+
45+
public Book createBook(Book book) throws Exception {
46+
Book createdBook = null;
47+
OkHttpClient httpclient = new OkHttpClient();
48+
ObjectMapper mapper = new ObjectMapper();
49+
String jsonBook = mapper.writeValueAsString(book);
50+
Request request = new Request.Builder().url(URI_BOOK).post(RequestBody.create(MediaTypeJSON, jsonBook)).build();
51+
try (Response response = httpclient.newCall(request).execute()) {
52+
53+
if (response.isSuccessful()) {
54+
// Get back the created book
55+
createdBook = mapper.readValue(response.body().bytes(), Book.class);
56+
57+
}
58+
}
59+
60+
return createdBook;
61+
}
62+
63+
public Book[] getAllBooks() throws Exception {
64+
Book[] books = null;
65+
OkHttpClient httpclient = new OkHttpClient();
66+
Request request = new Request.Builder().url(URI_BOOK).get().build();
67+
try (Response response = httpclient.newCall(request).execute()) {
68+
ObjectMapper mapper = new ObjectMapper();
69+
books = mapper.readValue(response.body().bytes(), Book[].class);
70+
}
71+
return books;
72+
}
73+
74+
public static void main(String[] args) throws Exception {
75+
BookRepositoryImplOkHttp bookRepository = new BookRepositoryImplOkHttp();
76+
// Getting the first book from the RESTful service
77+
Book book = bookRepository.getAllBooks()[0];
78+
bookRepository.deleteBook(book.getId());
79+
80+
}
81+
82+
public Book findBookById(Long id) {
83+
return null;
84+
}
85+
86+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.howtoprogram.repository.okhttp;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import com.howtoprogram.domain.Book;
7+
8+
import junit.framework.Assert;
9+
10+
public class BookRepositoryImplOkHttpTest {
11+
private BookRepositoryImplOkHttp bookRepository;
12+
13+
@Before
14+
public void initialize() {
15+
bookRepository = new BookRepositoryImplOkHttp();
16+
}
17+
18+
@Test
19+
public void testGetAllBooks() throws Exception {
20+
Book[] books = bookRepository.getAllBooks();
21+
Assert.assertTrue(books.length > 0);
22+
23+
}
24+
25+
@Test
26+
public void testDeleteBook() throws Exception {
27+
Book[] books1 = bookRepository.getAllBooks();
28+
bookRepository.deleteBook(books1[0].getId());
29+
Book[] books2 = bookRepository.getAllBooks();
30+
Assert.assertEquals(books1.length - 1, books2.length);
31+
32+
}
33+
34+
@Test
35+
public void testAddBook() throws Exception {
36+
Book book = new Book(null, "Hell", "aaa");
37+
Book createdBook = bookRepository.createBook(book);
38+
Assert.assertTrue(createdBook != null && createdBook.getId() > 0);
39+
}
40+
41+
@Test
42+
public void testUpdateBook() throws Exception {
43+
Book[] books = bookRepository.getAllBooks();
44+
Book book = books[0];
45+
book.setAuthor("Reactive Programming");
46+
Book updatedBook = bookRepository.updateBook(book);
47+
Assert.assertTrue(updatedBook != null);
48+
Assert.assertEquals("Reactive Programming", updatedBook.getAuthor());
49+
50+
}
51+
}

0 commit comments

Comments
 (0)