Project

General

Profile

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

chin-yeh, 12/02/2011 05:05 PM

1 1 chin-yeh
{{toc}}
2
3
h1. Programming Guide - Web Services Client (RESTful)
4
5
This guide describes how to develop the client for RESTful-based web services. And, it can be used in *Java project* and *Java Web Application project*.
6
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
* com.springsource.org.aopalliance-1.0.0.jar
137
* commons-codec-1.2.jar
138
* commons-httpclient-3.1.jar
139
* commons-lang-2.6.jar
140
* attachment:connector-client-utils.jar
141
* jcl-over-slf4j-1.6.1.jar
142
* org.springframework.aop-3.0.5.RELEASE.jar
143
* org.springframework.asm-3.0.5.RELEASE.jar
144
* org.springframework.beans-3.0.5.RELEASE.jar
145
* org.springframework.context.support-3.0.5.RELEASE.jar
146
* org.springframework.context-3.0.5.RELEASE.jar
147
* org.springframework.core-3.0.5.RELEASE.jar
148
* org.springframework.expression-3.0.5.RELEASE.jar
149
* org.springframework.web.servlet-3.0.5.RELEASE.jar
150
* org.springframework.web-3.0.5.RELEASE.jar
151
* slf4j-api-1.6.1.jar
152
* jackson-core-asl-1.9.0.jar
153
* jackson-jaxrs-1.9.0.jar
154
* jackson-mapper-asl-1.9.0.jar
155
* servlet-api-2.4.jar
156
* commons-io-2.1.jar
157
158
These dependencies can be found in:
159
* [[General Info:SearchBrowse_artifact|Nexus]]
160
* download this attachment:web_service_client_libs.zip and put it the *project's library folder*
161
162
163
h1. API Reference
164 3 chin-yeh
165
h2. Synchronous Web Service Client
166
167
_Method Signature:_
168
<pre>
169
<code class="JAVA">
170
public static <E, T> T invoke(WebServicesRequest<E, T> request)
171
</code>
172
</pre>
173
174
h3. Input Parameter
175
176
* *request* - the request object consists of
177
** <code>E</code> - the type of the request entity
178
** <code>T</code> - the type of the response entity
179
** *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
184
h3. Output Parameter
185
186
based on the provided input fields
187
188
h2. Asynschronous Web Service Client
189
190
_Method Signature:_
191
<pre>
192
<code class="JAVA">
193
public static <E, T> void persistAndInvoke(Connection connection,
194
			AsyncWebServicesRequest<E, T> request)
195
</code>
196
</pre>
197
198
h3. Input Parameter
199
200
* *request* - the request object consists of
201
** <code>E</code> - the type of the request entity
202
** <code>T</code> - the type of the response entity
203 4 chin-yeh
** *connection* - an established database connection. release it after done
204 3 chin-yeh
** *requestEntity* - the request entity, it can be a <code>String</code> or <code>Object</code>
205
** *requestUrl* - the URL of the web service
206
** *httpMethod* - either <code>POST</code>, <code>GET</code>, <code>PUT</code> and etc.
207
** *responseType* - the type of the response entity, it can be a <code>String</code> or <code>Object</code>
208 4 chin-yeh
** *asyncWebServiceUrl* - the request will first send this URL and then the "forwarder" will forward the request to *request URL*
209
** *callback URL* - this URL will capture the response of the submitted request
210
** _(optional)_ *mall indicator* - the indicator of the Mall which trigger the request
211
** _(optional)_ *transaction type* - the type of the request
212
** _(optional)_ *client transaction ID* - the client unique reference of the request
213 3 chin-yeh
214
215
216
h3. Output Parameter
217
218
based on the provided input fields