Actions
Programming Guide - Web Services Client (RESTful) » History » Revision 7
« Previous |
Revision 7/9
(diff)
| Next »
chin-yeh, 12/07/2011 03:06 PM
- Table of contents
- Programming Guide - Web Services Client (RESTful)
- API Reference
Programming Guide - Web Services Client (RESTful)¶
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.
There are 2 types of web services client:- Synchronous based - the client invoke the request and get the response in the same session
- Asynchronous based - the client invoke the request and get the response via the callback method (2 different sessions)
Refer to the below diagram to have a better understanding how the Web Services works:
Step-By-Step¶
Assume that you have setup the Eclipse environment.
This section contains 2 parts:- Web Services Client
- Web Services Client (Asynchronous)
- Callback of Web Services Invocation
Web Services Client¶
- launch Eclipse
- you can either:
- create a new Java Project, or
- JDK compiler compliance version should set to 5.0 or above
- create a new Dynamic Web Project, or
- Dynamic web module version should set to 2.4 or above
- JDK compiler compliance version should set to 5.0 or above
- use the existing project
- JDK compiler compliance version should set to 5.0 or above
- create a new Java Project, or
- import all of the dependencies into the project
- obtain the following information from the web service provider:
- refer to Input Parameter section
- use those information to construct the
WebServicesRequest<E, T>
request object, for example:WebServicesRequest<MemberInfo, Boolean> request = new WebServicesRequest<MemberInfo, Boolean>( memberInfo, Boolean.class); request.setHttpMethod(HttpMethod.POST); request.setRequestUrl("http://foo.bar.com/webapp/reg-services/shopper");
E
- is the type of the request entity, e.g.MemberInfo
T
- is the type of the response entity, e.g.Boolean
- note: if the request entity is empty or not needed, just pass in an empty
String
object, not null, e.g.:WebServicesRequest<String, MemberInfo> request = new WebServicesRequest<String, MemberInfo>("", MemberInfo.class);
- note: if the request entity is empty or not needed, just pass in an empty
- after constructed request object, pass it on to the WebServicesHelper.invoke method.
WebServicesRequest<MemberInfo, Boolean> request = createRegisterMemberRequest(); Boolean response = WebServicesHelper.invoke(request);
- done
Web Services Client (Asynchronous)¶
Web Service ClientSee also Web Services Client
- launch Eclipse
- you can either:
- create a new Java Project, or
- JDK compiler compliance version should set to 5.0 or above
- create a new Dynamic Web Project, or
- Dynamic web module version should set to 2.4 or above
- JDK compiler compliance version should set to 5.0 or above
- use the existing project
- JDK compiler compliance version should set to 5.0 or above
- create a new Java Project, or
- import all of the dependencies into the project
- obtain the following information from the web service provider:
- refer to Input Parameter section
- use those information to construct the
AsyncWebServicesRequest<E, T>
request object, for example:AsyncWebServicesRequest<MemberInfo, Boolean> request = new AsyncWebServicesRequest<MemberInfo, Boolean>(ASYNC_WS_URL, memberInfo, Boolean.class); request.setHttpMethod(HttpMethod.POST); request.setRequestUrl("http://foo.bar.com/reg-services/shopper"); request.setCallbackUrl("http://192.168.2.68:8080/dummy-mall/callback.jsp"); request.setMallIndicator("840"); request.setTrxType("MEMBER REGISTRATION"); request.setClientTrxId("REG-123");
E
- is the type of the request entity, e.g.MemberInfo
T
- is the type of the response entity, e.g.Boolean
- note: if the request entity is empty or not needed, just pass in an empty
String
object, not null, e.g.:AsyncWebServicesRequest<String, MemberInfo> request = new AsyncWebServicesRequest<String, MemberInfo>(ASYNC_WS_URL, "", MemberInfo.class);
- note: if the request entity is empty or not needed, just pass in an empty
- after constructed request object, pass it on to the WebServicesHelper.persistAndInvoke method.
Connection connection = createDbConnection(); try { WebServicesHelper.persistAndInvoke(connection, request); } finally { if (connection != null) { connection.close(); } }
- an established database connection is needed because the request will be stored into database before it actually get processed
- proceed to the next part to develop the callback component, which will be used to capture the response of the submitted request
- this component must be placed
- in the web application and,
- use the same database connection as the one that used in the application which triggered request and,
- accepts HTTP POST method
- after confirmed where to place the component, create a servlet or JSP and include the following codes:
WebServicesHelper.updateEventLogStatus(connection, request);
- connection - an established database connection. release it after used
- request -
an HTTPServletRequest
object
- this newly created servlet/JSP will be used as a callback URL for the application
- done
Project Dependencies¶
- com.springsource.org.aopalliance-1.0.0.jar
- commons-codec-1.2.jar
- commons-httpclient-3.1.jar
- commons-lang-2.6.jar
- connector-client-utils.jar
- jcl-over-slf4j-1.6.1.jar
- org.springframework.aop-3.0.5.RELEASE.jar
- org.springframework.asm-3.0.5.RELEASE.jar
- org.springframework.beans-3.0.5.RELEASE.jar
- org.springframework.context.support-3.0.5.RELEASE.jar
- org.springframework.context-3.0.5.RELEASE.jar
- org.springframework.core-3.0.5.RELEASE.jar
- org.springframework.expression-3.0.5.RELEASE.jar
- org.springframework.web.servlet-3.0.5.RELEASE.jar
- org.springframework.web-3.0.5.RELEASE.jar
- slf4j-api-1.6.1.jar
- jackson-core-asl-1.9.0.jar
- jackson-jaxrs-1.9.0.jar
- jackson-mapper-asl-1.9.0.jar
- servlet-api-2.4.jar
- commons-io-2.1.jar
- Nexus
- download this web_service_client_libs.zip and put it the project's library folder
API Reference¶
Synchronous Web Service Client¶
Method Signature:
public static <E, T> T invoke(WebServicesRequest<E, T> request)
Input Parameter¶
- request - the request object consists of
E
- the type of the request entityT
- the type of the response entity- requestEntity - the request entity, it can be a
String
orObject
- requestUrl - the URL of the web service
- httpMethod - either
POST
,GET
,PUT
and etc. - responseType - the type of the response entity, it can be a
String
orObject
Output Parameter¶
based on the provided input fields
Asynschronous Web Service Client¶
Method Signature:
public static <E, T> void persistAndInvoke(Connection connection, AsyncWebServicesRequest<E, T> request)
Input Parameter¶
- request - the request object consists of
E
- the type of the request entityT
- the type of the response entity- connection - an established database connection. release it after done
- requestEntity - the request entity, it can be a
String
orObject
- requestUrl - the URL of the web service
- httpMethod - either
POST
,GET
,PUT
and etc. - responseType - the type of the response entity, it can be a
String
orObject
- asyncWebServiceUrl - the request will first send this URL and then the "forwarder" will forward the request to request URL
- callback URL - this URL will capture the response of the submitted request
- (optional) mall indicator - the indicator of the Mall in which triggered the request
- (optional) transaction type - the type of the request
- (optional) client transaction ID - the client unique reference of the request
Output Parameter¶
based on the provided input fields
Updated by chin-yeh about 13 years ago · 7 revisions