Skip to content

Commit 59b8bec

Browse files
committed
add retrofit2
1 parent 367f508 commit 59b8bec

File tree

6 files changed

+208
-57
lines changed

6 files changed

+208
-57
lines changed

Java-RESTful-Client-Example/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@
2121
<attribute name="maven.pomderived" value="true"/>
2222
</attributes>
2323
</classpathentry>
24+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
25+
<attributes>
26+
<attribute name="optional" value="true"/>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
2430
<classpathentry kind="output" path="target/classes"/>
2531
</classpath>

Java-RESTful-Client-Example/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,23 @@
100100
<version>1.4.9</version>
101101
</dependency>
102102
<!-- End Unirest dependencies -->
103+
104+
<dependency>
105+
<groupId>com.squareup.retrofit2</groupId>
106+
<artifactId>retrofit</artifactId>
107+
<version>2.1.0</version>
108+
</dependency>
109+
<dependency>
110+
<groupId>com.squareup.retrofit2</groupId>
111+
<artifactId>converter-gson</artifactId>
112+
<version>2.1.0</version>
113+
</dependency>
114+
115+
<dependency>
116+
<groupId>junit</groupId>
117+
<artifactId>junit</artifactId>
118+
<version>4.8.1</version>
119+
<scope>test</scope>
120+
</dependency>
103121
</dependencies>
104122
</project>
Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,69 @@
11
package com.howtoprogram.domain;
22

3-
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
4-
5-
@JacksonXmlRootElement(localName = "book")
63
public class Book {
7-
private Long id;
8-
private String name;
9-
private String author;
10-
11-
12-
public Book() {
13-
super();
14-
}
4+
private Long id;
5+
private String name;
6+
private String author;
157

16-
public Book(Long id, String name, String author) {
17-
super();
18-
this.id = id;
19-
this.name = name;
20-
this.author = author;
21-
}
8+
public Book() {
9+
super();
10+
}
2211

23-
@Override
24-
public String toString() {
25-
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
26-
}
12+
public Book(Long id, String name, String author) {
13+
super();
14+
this.id = id;
15+
this.name = name;
16+
this.author = author;
17+
}
2718

28-
/**
29-
* @return the id
30-
*/
31-
public Long getId() {
32-
return id;
33-
}
19+
@Override
20+
public String toString() {
21+
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
22+
}
3423

35-
/**
36-
* @param id the id to set
37-
*/
38-
public void setId(Long id) {
39-
this.id = id;
40-
}
24+
/**
25+
* @return the id
26+
*/
27+
public Long getId() {
28+
return id;
29+
}
4130

42-
/**
43-
* @return the name
44-
*/
45-
public String getName() {
46-
return name;
47-
}
31+
/**
32+
* @param id
33+
* the id to set
34+
*/
35+
public void setId(Long id) {
36+
this.id = id;
37+
}
4838

49-
/**
50-
* @param name the name to set
51-
*/
52-
public void setName(String name) {
53-
this.name = name;
54-
}
39+
/**
40+
* @return the name
41+
*/
42+
public String getName() {
43+
return name;
44+
}
5545

56-
/**
57-
* @return the author
58-
*/
59-
public String getAuthor() {
60-
return author;
61-
}
46+
/**
47+
* @param name
48+
* the name to set
49+
*/
50+
public void setName(String name) {
51+
this.name = name;
52+
}
6253

63-
/**
64-
* @param author the author to set
65-
*/
66-
public void setAuthor(String author) {
67-
this.author = author;
68-
}
54+
/**
55+
* @return the author
56+
*/
57+
public String getAuthor() {
58+
return author;
59+
}
6960

61+
/**
62+
* @param author
63+
* the author to set
64+
*/
65+
public void setAuthor(String author) {
66+
this.author = author;
67+
}
7068

7169
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.howtoprogram.repository.retrofit2;
2+
3+
import java.util.List;
4+
5+
import com.howtoprogram.domain.Book;
6+
7+
import retrofit2.Call;
8+
import retrofit2.Retrofit;
9+
import retrofit2.converter.gson.GsonConverterFactory;
10+
11+
public class BookRepositoryImplRetrofit2 {
12+
private static final String URI_BOOK = "http://localhost:8080";
13+
14+
public Book updateBook(Book book) throws Exception {
15+
Retrofit retrofit = new Retrofit.Builder().baseUrl(URI_BOOK).addConverterFactory(GsonConverterFactory.create())
16+
.build();
17+
BookResourceRetrofit2 service = retrofit.create(BookResourceRetrofit2.class);
18+
19+
return service.updateBook(book.getId(), book).execute().body();
20+
}
21+
22+
public Book createBook(Book book) throws Exception {
23+
Retrofit retrofit = new Retrofit.Builder().baseUrl(URI_BOOK).addConverterFactory(GsonConverterFactory.create())
24+
.build();
25+
BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class);
26+
return bookResource.createBook(book).execute().body();
27+
28+
}
29+
30+
public List<Book> getAllBooks() throws Exception {
31+
Retrofit retrofit = new Retrofit.Builder().baseUrl(URI_BOOK).addConverterFactory(GsonConverterFactory.create())
32+
.build();
33+
BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class);
34+
Call<List<Book>> books = bookResource.getAllBooks();
35+
return books.execute().body();
36+
}
37+
38+
public static void main(String[] args) throws Exception {
39+
BookRepositoryImplRetrofit2 bookRepository = new BookRepositoryImplRetrofit2();
40+
Book book = bookRepository.getAllBooks().get(0);
41+
System.out.println(book);
42+
// bookRepository.deleteBook(book.getId());
43+
}
44+
45+
public void deleteBook(Long id) {
46+
Retrofit retrofit = new Retrofit.Builder().baseUrl(URI_BOOK).addConverterFactory(GsonConverterFactory.create())
47+
.build();
48+
BookResourceRetrofit2 bookResource = retrofit.create(BookResourceRetrofit2.class);
49+
bookResource.deleteBook(id);
50+
}
51+
52+
public Book findBookById(Long id) {
53+
return null;
54+
}
55+
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.howtoprogram.repository.retrofit2;
2+
3+
import java.util.List;
4+
5+
import com.howtoprogram.domain.Book;
6+
7+
import retrofit2.Call;
8+
import retrofit2.http.Body;
9+
import retrofit2.http.DELETE;
10+
import retrofit2.http.GET;
11+
import retrofit2.http.Header;
12+
import retrofit2.http.Headers;
13+
import retrofit2.http.POST;
14+
import retrofit2.http.PUT;
15+
import retrofit2.http.Path;
16+
17+
public interface BookResourceRetrofit2 {
18+
@Headers({ "Accept: application/json" })
19+
@GET("v1/books")
20+
Call<List<Book>> getAllBooks();
21+
22+
@POST("v1/books")
23+
Call<Book> createBook(@Body Book book);
24+
25+
@PUT("v1/books/{id}")
26+
Call<Book> updateBook(@Path("id") Long id, @Body Book book);
27+
28+
@DELETE("v1/books/{id}")
29+
void deleteBook(@Path("id") Long id);
30+
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.howtoprogram.repository.retrofit2;
2+
3+
import java.util.List;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import com.howtoprogram.domain.Book;
9+
10+
import junit.framework.Assert;
11+
12+
public class BookRepositoryImplRetrofit2Test {
13+
private BookRepositoryImplRetrofit2 bookRepositoryImplRetrofit2;
14+
15+
@Before
16+
public void initialize() {
17+
bookRepositoryImplRetrofit2 = new BookRepositoryImplRetrofit2();
18+
}
19+
20+
@Test
21+
public void testGetAllBooks() throws Exception {
22+
List<Book> books = bookRepositoryImplRetrofit2.getAllBooks();
23+
Assert.assertTrue(books.size() > 0);
24+
25+
}
26+
27+
@Test
28+
public void testAddBook() throws Exception {
29+
Book book = new Book(null, "Hell", "aaa");
30+
Book books = bookRepositoryImplRetrofit2.createBook(book);
31+
Assert.assertTrue(book != null);
32+
33+
}
34+
35+
@Test
36+
public void testUpdateBook() throws Exception {
37+
Book book = new Book(null, "Hell", "aaa");
38+
Book books = bookRepositoryImplRetrofit2.createBook(book);
39+
Assert.assertTrue(book != null);
40+
41+
}
42+
}

0 commit comments

Comments
 (0)