Skip to content

Commit 155d17e

Browse files
committed
implemented by Apache HttpComponents
1 parent 3a21a8c commit 155d17e

File tree

6 files changed

+74
-79
lines changed

6 files changed

+74
-79
lines changed

Book-RESTful-Service/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Open the **Application.java**
1818

1919
This RESTful ws provides API for all below sub projects:
2020
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
21-
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
21+
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
22+
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)

Java-JsonPojo-Example/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Source code are described in **[Convert Java Objects To JSON And Vice Versa](htt
1818

1919
## Related articles
2020
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
21-
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
21+
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
22+
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)

Java-RESTful-Client-Example/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ Click **Finish** button to finish the importing
1414
The **BookRepositoryImplRaw.java** includes all raw examples which we create content as XML, JSON and postto RESTful WS
1515
The **BookRepositoryImplJackson.java** includes all raw examples which we use Jackson to convert objects to XML, JSON before sending RESTful WS
1616
The **BookRepositoryImplSpring.java** includes all raw examples about creating RESTful Client With Spring RestTemplate.
17+
The **BookRepositoryImplApacheHttpClient.java** includes all raw examples about creating RESTful Client With Apache Httpcomponents
1718

1819
Source code are described:
1920
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
2021
###[Java RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
2122
## 3. Related articles
2223
###[Convert Java Objects To JSON And Vice Versa](http://howtoprogram.xyz/2016/07/01/convert-java-objects-json-vice-versa/)
23-
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
24+
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
25+
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)
Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<groupId>com.howtoprogram</groupId>
5-
<artifactId>java-restful-client-example</artifactId>
6-
<version>0.0.1-SNAPSHOT</version>
7-
<properties>
8-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9-
<maven.compiler.source>1.8</maven.compiler.source>
10-
<maven.compiler.target>1.8</maven.compiler.target>
11-
</properties>
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.howtoprogram</groupId>
5+
<artifactId>java-restful-client-example</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
<maven.compiler.source>1.8</maven.compiler.source>
10+
<maven.compiler.target>1.8</maven.compiler.target>
11+
</properties>
1212

13-
<dependencies>
14-
<dependency>
15-
<groupId>com.fasterxml.jackson.core</groupId>
16-
<artifactId>jackson-databind</artifactId>
17-
<version>2.7.5</version>
18-
</dependency>
19-
<dependency>
20-
<groupId>com.fasterxml.jackson.dataformat</groupId>
21-
<artifactId>jackson-dataformat-xml</artifactId>
22-
<version>2.6.3</version>
23-
</dependency>
24-
<dependency>
25-
<groupId>org.springframework</groupId>
26-
<artifactId>spring-web</artifactId>
27-
<version>4.2.6.RELEASE</version>
28-
</dependency>
29-
<dependency>
30-
<groupId>org.apache.httpcomponents</groupId>
31-
<artifactId>httpclient</artifactId>
32-
<version>4.5.2</version>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.apache.httpcomponents</groupId>
36-
<artifactId>httpasyncclient</artifactId>
37-
<version>4.1.2</version>
38-
</dependency>
39-
<dependency>
40-
<groupId>org.slf4j</groupId>
41-
<artifactId>slf4j-api</artifactId>
42-
<version>1.7.21</version>
43-
</dependency>
44-
<dependency>
45-
<groupId>ch.qos.logback</groupId>
46-
<artifactId>logback-classic</artifactId>
47-
<version>1.1.7</version>
48-
</dependency>
49-
</dependencies>
13+
<dependencies>
14+
<dependency>
15+
<groupId>com.fasterxml.jackson.core</groupId>
16+
<artifactId>jackson-databind</artifactId>
17+
<version>2.7.5</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.fasterxml.jackson.dataformat</groupId>
21+
<artifactId>jackson-dataformat-xml</artifactId>
22+
<version>2.6.3</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-web</artifactId>
27+
<version>4.2.6.RELEASE</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.apache.httpcomponents</groupId>
32+
<artifactId>httpasyncclient</artifactId>
33+
<version>4.1.2</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.slf4j</groupId>
37+
<artifactId>slf4j-api</artifactId>
38+
<version>1.7.21</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>ch.qos.logback</groupId>
42+
<artifactId>logback-classic</artifactId>
43+
<version>1.1.7</version>
44+
</dependency>
45+
</dependencies>
5046
</project>

Java-RESTful-Client-Example/src/main/java/com/howtoprogram/service/BookRepositoryImplApacheHttpClient.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
public class BookRepositoryImplApacheHttpClient {
2121
private static final String URI_BOOK = "http://localhost:8080/v1/books";
2222

23-
public boolean deleteBook(Long id) {
24-
boolean retVal = false;
23+
public void deleteBook(Long id) throws Exception {
2524
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
2625
try {
2726
httpclient.start();
@@ -32,18 +31,16 @@ public boolean deleteBook(Long id) {
3231
HttpResponse response = future.get();
3332
System.out.println("Response code:" + response.getStatusLine().getStatusCode());
3433
// Determine whether the request was successfully or not
35-
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
36-
retVal = true;
34+
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) {
35+
throw new RuntimeException("Failed to delete the book with id:" + id);
3736
}
38-
} catch (Exception e) {
39-
e.printStackTrace();
4037
} finally {
4138
HttpAsyncClientUtils.closeQuietly(httpclient);
4239
}
43-
return retVal;
4440
}
41+
4542

46-
public Book updateBook(Book book) {
43+
public Book updateBook(Book book) throws Exception {
4744

4845
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
4946
Book createdBook = null;
@@ -67,18 +64,16 @@ public Book updateBook(Book book) {
6764
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
6865
// Get back the updated book
6966
createdBook = mapper.readValue(response.getEntity().getContent(), Book.class);
70-
7167
}
72-
} catch (Exception e) {
73-
e.printStackTrace();
68+
7469
} finally {
7570
HttpAsyncClientUtils.closeQuietly(httpclient);
7671
}
7772
return createdBook;
7873

7974
}
8075

81-
public Book createBook(Book book) {
76+
public Book createBook(Book book) throws Exception {
8277

8378
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
8479
Book createdBook = null;
@@ -104,26 +99,15 @@ public Book createBook(Book book) {
10499
createdBook = mapper.readValue(response.getEntity().getContent(), Book.class);
105100

106101
}
107-
} catch (Exception e) {
108-
e.printStackTrace();
102+
109103
} finally {
110104
HttpAsyncClientUtils.closeQuietly(httpclient);
111105
}
112106
return createdBook;
113107

114108
}
115109

116-
public static void main(String[] args) {
117-
BookRepositoryImplApacheHttpClient repository = new BookRepositoryImplApacheHttpClient();
118-
// Getting the first book from the RESTful service
119-
Book book = repository.getAllBooks()[0];
120-
System.out.println(book);
121-
122-
}
123-
124-
125-
public Book[] getAllBooks() {
126-
110+
public Book[] getAllBooks() throws Exception {
127111
Book[] books = null;
128112
// Create an asyn HttpClient
129113
CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
@@ -142,14 +126,24 @@ public Book[] getAllBooks() {
142126
// Read the inputstream and convert to an array of Book
143127
books = mapper.readValue(httpEntity.getContent(), Book[].class);
144128
}
145-
} catch (Exception ex) {
146-
ex.printStackTrace();
147129
} finally {
148130
HttpAsyncClientUtils.closeQuietly(httpclient);
149131
}
150132
return books;
151133
}
152134

135+
136+
137+
public static void main(String[] args) throws Exception {
138+
BookRepositoryImplApacheHttpClient bookRepository = new BookRepositoryImplApacheHttpClient();
139+
// Getting the first book from the RESTful service
140+
Book book = bookRepository.getAllBooks()[0];
141+
bookRepository.deleteBook(book.getId());
142+
143+
}
144+
145+
146+
153147
public Book findBookById(Long id) {
154148
return null;
155149
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Java Examples
22

3-
## Java-JsonPojo-Example
3+
## Java JsonPojo Example
44
This small project contains examples how to convert Java object, Hashmap, List to JSON and vice versa. It also contains examples about how to work with files.
55
###[Convert Java Objects To JSON And Vice Versa](http://howtoprogram.xyz/2016/07/01/convert-java-objects-json-vice-versa/)
66
## Simple Java RESTful Client Example
77
This projects contains some examples about plementing RESTful Web Service Clients using *java.net.URL* package
88
###[Simple Java RESTful Web Service Clients](http://howtoprogram.xyz/2016/07/02/java-restful-web-service-clients/)
99
## Implementing RESTful Web Service Clients using Spring RestTemplate
10-
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
10+
###[RESTful Client With Spring RestTemplate](http://howtoprogram.xyz/2016/07/03/java-restful-client-spring-resttemplate/)
11+
###[Java RESTful Client With Apache Httpcomponents](http://howtoprogram.xyz/2016/07/04/java-restful-client-spring-apache-httpcomponents/)

0 commit comments

Comments
 (0)