Project

General

Profile

Programming Guide - Web Services Client (RESTful) » History » Version 9

chin-yeh, 01/03/2012 10:23 AM

1 1 chin-yeh
{{toc}}
2
3
h1. Programming Guide - Web Services Client (RESTful)
4
5 9 chin-yeh
This guide describes how to develop the client for RESTful-based web services. And, it's applicable to *Java project* and *Java Web Application project*.
6 1 chin-yeh
7
There are 2 types of web services client:
8
* [[Programming_Guide_-_Web_Services_Client_(RESTful)#Step-By-Step|Synchronous based]] - the client invoke the request and get the response in the *same session*
9
* [[Programming_Guide_-_Web_Services_Client_(RESTful)#Step-By-Step|Asynchronous based]] - the client invoke the request and get the response via the _callback_ method _(2 different sessions)_
10
11 2 chin-yeh
Refer to the below diagram to have a better understanding how the Web Services works:
12
!rest_json_with_queue.png!
13 1 chin-yeh
14
h2. Step-By-Step
15
16
Assume that you have setup the [[General Info:Setup Development Environment|Eclipse environment]].
17
18
This section contains 2 parts:
19
* Web Services Client
20
* Web Services Client (Asynchronous)
21
** Callback of Web Services Invocation
22
23
h3. Web Services Client
24
25
# launch *Eclipse*
26
# you can either:
27
** create a new *Java Project*, or
28
*** _JDK_ compiler compliance version should set to _5.0_ or above
29
** create a new *Dynamic Web Project*, or
30
*** _Dynamic web module version_ should set to _2.4_ or above
31
*** _JDK_ compiler compliance version should set to _5.0_ or above
32
** use the existing project
33
*** _JDK_ compiler compliance version should set to _5.0_ or above
34
# import all of the [[Programming_Guide_-_Web_Services_Client_(RESTful)#Project-Dependencies|dependencies]] into the project 
35
# obtain the following information from the web service provider:
36 5 chin-yeh
** refer to [[Programming_Guide_-_Web_Services_Client_(RESTful)#Synchronous-Web-Service-Client|Input Parameter]] section
37 1 chin-yeh
# use those information to construct the <code>WebServicesRequest<E, T></code> request object, for example:
38
<pre>
39
<code class="JAVA">
40
WebServicesRequest<MemberInfo, Boolean> request = new WebServicesRequest<MemberInfo, Boolean>(
41
					memberInfo, Boolean.class);
42
request.setHttpMethod(HttpMethod.POST);
43
request.setRequestUrl("http://foo.bar.com/webapp/reg-services/shopper");
44
</code>
45
</pre>
46
** <code>E</code> - is the *type* of the request entity, e.g. <code>MemberInfo</code>
47
** <code>T</code> - is the *type* of the response entity, e.g. <code>Boolean</code>
48
*** note: if the request entity is *empty* or not needed, just pass in an empty <code>String</code> object, *not null*, e.g.:
49
<pre>
50
<code class="JAVA">
51
WebServicesRequest<String, MemberInfo> request = new WebServicesRequest<String, MemberInfo>("", MemberInfo.class);
52
</code>
53
</pre>
54 5 chin-yeh
# after constructed request object, pass it on to the [[Programming_Guide_-_Web_Services_Client_(RESTful)#Synchronous-Web-Service-Client|WebServicesHelper.invoke]] method.
55 1 chin-yeh
<pre>
56
<code class="JAVA">
57
WebServicesRequest<MemberInfo, Boolean> request = createRegisterMemberRequest();
58
		
59
Boolean response = WebServicesHelper.invoke(request);
60
</code>
61
</pre>
62
# done
63
64
h3. Web Services Client (Asynchronous)
65
66
> See also [[Programming_Guide_-_Web_Services_Client_(RESTful)#Web-Services-Client|Web Services Client]]
67
68
_Web Service Client_
69
# launch *Eclipse*
70
# you can either:
71
** create a new *Java Project*, or
72
*** _JDK_ compiler compliance version should set to _5.0_ or above
73
** create a new *Dynamic Web Project*, or
74
*** _Dynamic web module version_ should set to _2.4_ or above
75
*** _JDK_ compiler compliance version should set to _5.0_ or above
76
** use the existing project
77
*** _JDK_ compiler compliance version should set to _5.0_ or above
78
# import all of the [[Programming_Guide_-_Web_Services_Client_(RESTful)#Project-Dependencies|dependencies]] into the project 
79
# obtain the following information from the web service provider:
80 5 chin-yeh
** refer to [[Programming_Guide_-_Web_Services_Client_(RESTful)#Asynschronous-Web-Service-Client|Input Parameter]] section
81 1 chin-yeh
# use those information to construct the <code>AsyncWebServicesRequest<E, T></code> request object, for example:
82
<pre>
83
<code class="JAVA">
84
AsyncWebServicesRequest<MemberInfo, Boolean> request = new AsyncWebServicesRequest<MemberInfo, Boolean>(ASYNC_WS_URL,
85
					memberInfo, Boolean.class);
86
request.setHttpMethod(HttpMethod.POST);
87
request.setRequestUrl("http://foo.bar.com/reg-services/shopper");
88
request.setCallbackUrl("http://192.168.2.68:8080/dummy-mall/callback.jsp");
89
request.setMallIndicator("840");
90
request.setTrxType("MEMBER REGISTRATION");
91
request.setClientTrxId("REG-123");
92
</code>
93
</pre>
94
** <code>E</code> - is the *type* of the request entity, e.g. <code>MemberInfo</code>
95
** <code>T</code> - is the *type* of the response entity, e.g. <code>Boolean</code>
96
*** note: if the request entity is *empty* or not needed, just pass in an empty <code>String</code> object, *not null*, e.g.:
97
<pre>
98
<code class="JAVA">
99
AsyncWebServicesRequest<String, MemberInfo> request = new AsyncWebServicesRequest<String, MemberInfo>(ASYNC_WS_URL, "", MemberInfo.class);
100
</code>
101
</pre>
102 6 chin-yeh
# after constructed request object, pass it on to the [[Programming_Guide_-_Web_Services_Client_(RESTful)#Asynschronous-Web-Service-Client|WebServicesHelper.persistAndInvoke]] method.
103 1 chin-yeh
<pre>
104
<code class="JAVA">
105
Connection connection = createDbConnection();
106
try {
107
	WebServicesHelper.persistAndInvoke(connection, request);
108
} finally {
109
	if (connection != null) {
110
		connection.close();
111
	}
112
}
113
</code>
114
</pre>
115
** *an established database connection is needed* because the request will be stored into database before it actually get processed
116
# *proceed to the next part* to develop the callback component, which will be used to capture the response of the submitted request
117
118
_Callback component_
119
# this component must be placed 
120
** in the *web application* and, 
121
** use the same *database connection* as the one that used in the application which triggered request and,
122
** accepts HTTP *POST*  method
123
# after confirmed where to place the component, create a *servlet* or *JSP* and include the following codes:
124
<pre>
125
<code class="JAVA">
126
WebServicesHelper.updateEventLogStatus(connection, request);
127
</code>
128
</pre>
129
** *connection* - an established database connection. release it after used
130
** *request* - <code>an HTTPServletRequest</code> object
131
# this newly created *servlet/JSP* will be used as a *callback URL* for the application
132
# done
133
134
h2. Project Dependencies
135
136 8 chin-yeh
Refer to [[Deployment Guide - connector-client-utils]]
137 1 chin-yeh
138
h1. API Reference
139 3 chin-yeh
140
h2. Synchronous Web Service Client
141
142
_Method Signature:_
143
<pre>
144
<code class="JAVA">
145
public static <E, T> T invoke(WebServicesRequest<E, T> request)
146
</code>
147
</pre>
148
149
h3. Input Parameter
150
151
* *request* - the request object consists of
152
** <code>E</code> - the type of the request entity
153
** <code>T</code> - the type of the response entity
154
** *requestEntity* - the request entity, it can be a <code>String</code> or <code>Object</code>
155
** *requestUrl* - the URL of the web service
156
** *httpMethod* - either <code>POST</code>, <code>GET</code>, <code>PUT</code> and etc.
157
** *responseType* - the type of the response entity, it can be a <code>String</code> or <code>Object</code>
158
159
h3. Output Parameter
160
161
based on the provided input fields
162
163
h2. Asynschronous Web Service Client
164
165
_Method Signature:_
166
<pre>
167
<code class="JAVA">
168
public static <E, T> void persistAndInvoke(Connection connection,
169
			AsyncWebServicesRequest<E, T> request)
170
</code>
171
</pre>
172
173
h3. Input Parameter
174
175
* *request* - the request object consists of
176
** <code>E</code> - the type of the request entity
177
** <code>T</code> - the type of the response entity
178 4 chin-yeh
** *connection* - an established database connection. release it after done
179 3 chin-yeh
** *requestEntity* - the request entity, it can be a <code>String</code> or <code>Object</code>
180
** *requestUrl* - the URL of the web service
181
** *httpMethod* - either <code>POST</code>, <code>GET</code>, <code>PUT</code> and etc.
182
** *responseType* - the type of the response entity, it can be a <code>String</code> or <code>Object</code>
183 4 chin-yeh
** *asyncWebServiceUrl* - the request will first send this URL and then the "forwarder" will forward the request to *request URL*
184
** *callback URL* - this URL will capture the response of the submitted request
185 7 chin-yeh
** _(optional)_ *mall indicator* - the indicator of the Mall in which triggered the request
186 4 chin-yeh
** _(optional)_ *transaction type* - the type of the request
187
** _(optional)_ *client transaction ID* - the client unique reference of the request
188 3 chin-yeh
189
190
191
h3. Output Parameter
192
193
based on the provided input fields