forked from howtoprogram/Java-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookRepositoryImplRaw.java
More file actions
131 lines (107 loc) · 3.7 KB
/
Copy pathBookRepositoryImplRaw.java
File metadata and controls
131 lines (107 loc) · 3.7 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.howtoprogram.restful;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.howtoprogram.domain.Book;
public class BookRepositoryImplRaw {
/**
* Get all {@link Book} from RESTful Web Service
*/
public String getAllBooksAsJson() {
HttpURLConnection connection = null;
BufferedReader reader = null;
String retVal = null;
try {
URL resetEndpoint = new URL("http://localhost:8080/v1/books");
connection = (HttpURLConnection) resetEndpoint.openConnection();
// Set request method to GET as required from the API
connection.setRequestMethod("GET");
// Read the response
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder jsonSb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
jsonSb.append(line);
}
retVal = jsonSb.toString();
// print out the json response
System.out.println(retVal);
} catch (Exception e) {
e.printStackTrace();
} finally {
// Clean up
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return retVal;
}
/**
* Creates {@link Book} by posting the XML to RESTful Web Service
*/
public void createBookAsXML() {
try {
URL url = new URL("http://localhost:8080/v1/books");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Set the request method to POST as required from the API
con.setRequestMethod("POST");
// Set the Content-Type to "application/xml" as required from the API
con.setRequestProperty("Content-Type", "application/xml");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
// The book we want to create in JSON format
String book = "<book><name>Effective Java</name><author>Joshua Bloch</author></book>";
os.write(book.getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code :" + responseCode);
if (responseCode == HttpURLConnection.HTTP_CREATED) {
System.out.println("Created book successfully.");
} else {
System.out.println("Created book failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates {@link Book} by posting the JSON to RESTful Web Service
*/
public void createBookAsJSON() {
try {
URL url = new URL("http://localhost:8080/v1/books");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Set the request method to POST as required from the API
con.setRequestMethod("POST");
// Set the Content-Type to "application/json" as required from the API
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
// The book we want to create in JSON format
String book = "{\"name\":\"Effective Java\",\"author\":\"Joshua Bloch\"}";
os.write(book.getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
System.out.println("Response Code :" + responseCode);
if (responseCode == HttpURLConnection.HTTP_CREATED) {
System.out.println("Created book successfully.");
} else {
System.out.println("Created book failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}