Skip to content

Commit 367f508

Browse files
committed
update unirest api example
1 parent 1ec0421 commit 367f508

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

Book-RESTful-Service/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ This RESTful ws provides API for all below sub projects:
2424
###[Java REST Client Using Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-rest-client-using-resteasy-client/)
2525
###[Java REST Client Using Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-rest-client-using-resteasy-client-proxy-framework/)
2626
###[Java REST Client Using Apache CXF Proxy based API](http://howtoprogram.xyz/2016/07/15/java-rest-client-using-apache-cxf-proxy-based-api/)
27-
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
27+
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
28+
###[Java REST Client Using Unirest Java API](http://howtoprogram.xyz/2016/07/27/java-rest-client-using-unirest-java-api/)

Java-JsonPojo-Example/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ Source code are described in **[Convert Java Objects To JSON And Vice Versa](htt
2424
###[Java REST Client Using Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-rest-client-using-resteasy-client/)
2525
###[Java REST Client Using Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-rest-client-using-resteasy-client-proxy-framework/)
2626
###[Java REST Client Using Apache CXF Proxy based API](http://howtoprogram.xyz/2016/07/15/java-rest-client-using-apache-cxf-proxy-based-api/)
27-
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
27+
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
28+
###[Java REST Client Using Unirest Java API](http://howtoprogram.xyz/2016/07/27/java-rest-client-using-unirest-java-api/)

Java-RESTful-Client-Example/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The **BookRepositoryImplResteasyProxy.java, SimpleResteasyProxyClient.java** inc
2121
The **BookRepositoryImplCXFProxy.java, BookResource.java** includes all raw examples about creating Java Java REST Client Using Apache CXF Proxy-based API
2222
Framework.
2323
The **BookResourceFeign.java, BookRepositoryImplFeign.java** includes all raw examples about creating Java Java REST Client Using Netflix Feign
24+
The **BookRepositoryImplUnirest.java** includes all raw examples about creating Java Java REST Client Using Unirest Java API
2425

2526
Source code are described:
2627
###[Convert Java Objects To JSON And Vice Versa](http://howtoprogram.xyz/2016/07/01/convert-java-objects-json-vice-versa/)
@@ -31,4 +32,5 @@ Source code are described:
3132
###[Java REST Client Using Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-rest-client-using-resteasy-client/)
3233
###[Java REST Client Using Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-rest-client-using-resteasy-client-proxy-framework/)
3334
###[Java REST Client Using Apache CXF Proxy based API](http://howtoprogram.xyz/2016/07/15/java-rest-client-using-apache-cxf-proxy-based-api/)
34-
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
35+
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
36+
###[Java REST Client Using Unirest Java API](http://howtoprogram.xyz/2016/07/27/java-rest-client-using-unirest-java-api/)

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/repository/unirest/BookRepositoryImplUnirest.java

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@
1111
public class BookRepositoryImplUnirest {
1212
private static final String URI_BOOK = "http://localhost:8080/v1/books";
1313

14+
public Book updateBook(Book book) throws Exception {
15+
HttpResponse<Book> response = Unirest.put(URI_BOOK + "/{id}")
16+
.routeParam("id", String.valueOf(book.getId())).header("accept", "application/json")
17+
.header("Content-Type", "application/json").body(book).asObject(Book.class);
18+
int status = response.getStatus();
19+
System.out.println("Status code: " + status);
20+
Book updatedBook = response.getBody();
21+
return updatedBook;
22+
}
23+
24+
25+
public void deleteBook(Long id) throws Exception {
26+
HttpResponse<String> response =
27+
Unirest.delete(URI_BOOK + "/{id}").routeParam("id", String.valueOf(id)).asString();
28+
System.out.println("Status code:" + response.getStatus());
29+
}
30+
31+
32+
33+
public Book createBook(Book book) throws Exception {
34+
HttpResponse<Book> response = Unirest.post(URI_BOOK).header("accept", "application/json")
35+
.header("Content-Type", "application/json").body(book).asObject(Book.class);
36+
int status = response.getStatus();
37+
System.out.println("Status code: " + status);
38+
Book createdBook = response.getBody();
39+
return createdBook;
40+
41+
}
42+
43+
public Book[] getAllBooks() throws Exception {
44+
HttpResponse<Book[]> response = Unirest.get(URI_BOOK).asObject(Book[].class);
45+
Book[] books = response.getBody();
46+
return books;
47+
}
48+
1449
public BookRepositoryImplUnirest() {
1550
Unirest.setObjectMapper(new ObjectMapper() {
1651
private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper =
@@ -34,50 +69,20 @@ public String writeValue(Object value) {
3469
});
3570
}
3671

37-
public void deleteBook(Long id) throws Exception {
38-
HttpResponse<String> response =
39-
Unirest.delete(URI_BOOK + "/{id}").routeParam("id", String.valueOf(id)).asString();
40-
System.out.println("Status code:" + response.getStatus());
41-
}
72+
4273

4374
public static void main(String[] args) throws Exception {
4475
BookRepositoryImplUnirest bookRepository = new BookRepositoryImplUnirest();
4576
// Getting the first book from the RESTful service
4677
Book book = bookRepository.getAllBooks()[0];
47-
System.out.println(book);
48-
bookRepository.deleteBook(book.getId());
49-
50-
}
51-
52-
public Book updateBook(Book book) throws Exception {
53-
HttpResponse<Book> response = Unirest.put(URI_BOOK).header("accept", "application/json")
54-
.header("Content-Type", "application/json").body(book).asObject(Book.class);
55-
int status = response.getStatus();
56-
System.out.println("Status code: " + status);
57-
Book updatedBook = response.getBody();
58-
return updatedBook;
59-
}
60-
61-
62-
public Book createBook(Book book) throws Exception {
63-
HttpResponse<Book> response = Unirest.post(URI_BOOK).header("accept", "application/json")
64-
.header("Content-Type", "application/json").body(book).asObject(Book.class);
65-
int status = response.getStatus();
66-
System.out.println("Status code: " + status);
67-
Book createdBook = response.getBody();
68-
return createdBook;
78+
book.setAuthor("111");
6979

80+
book = bookRepository.updateBook(book);
81+
System.out.println(book);
7082
}
7183

7284

7385

74-
public Book[] getAllBooks() throws Exception {
75-
HttpResponse<Book[]> response = Unirest.get(URI_BOOK).asObject(Book[].class);
76-
Book[] books = response.getBody();
77-
return books;
78-
}
79-
80-
8186
public Book findBookById(Long id) {
8287
return null;
8388
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ This RESTful ws provides API for all below sub projects
1414
###[Java REST Client Using Resteasy Client](http://howtoprogram.xyz/2016/07/12/java-rest-client-using-resteasy-client/)
1515
###[Java REST Client Using Resteasy Client Proxy Framework](http://howtoprogram.xyz/2016/07/13/java-rest-client-using-resteasy-client-proxy-framework/)
1616
###[Java REST Client Using Apache CXF Proxy based API](http://howtoprogram.xyz/2016/07/15/java-rest-client-using-apache-cxf-proxy-based-api/)
17-
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
17+
###[Java REST Client Using Netflix Feign](http://howtoprogram.xyz/2016/07/18/java-rest-client-using-netflix-feign/)
18+
###[Java REST Client Using Unirest Java API](http://howtoprogram.xyz/2016/07/27/java-rest-client-using-unirest-java-api/)

0 commit comments

Comments
 (0)