Lesson 3: Expose the RPC Adapter Service
In this lesson you will learn how to create the RPC Adapter service that returns a JSON response.
To expose the Movie service:
- Create the ShowMovies.jsp Web page:
- In the Enterprise Explorer view, expand
web2Project.
- Right-click
WebContent and select
New | Web Page. The
New Web Page wizard opens.
- In the File Name field, type ShowMovies.
- From the Template list, click
Basic Templates | JSP. Click Finish. ShowMovies.jsp is created and opens in the editor.
- Expose the RPC Adapter Service:
- In the Page Data view (
Window | Show View | Page Data), right-click
RPC Adapter Services and select
New | RPC Adapter Service. The
Expose RPC Adapter Service window opens.
- In the Class field, click Browse. The
Select a Java class to expose dialog opens.
- In the Select entries field, type MovieService. In the Matching items list, click
MovieService - web2. Click OK.
- In the Methods list, click
getMovieList(), then click Next.
- In the URL section, the URL that is displayed is the URL that calls your getMovieList method when it is invoked:
Click Finish.
The RPC Adapter Service is created in the Page Data view.
The RPC Adapter configuration file, RpcAdapterConfig.xml, is created and the getMovieList() is exposed.
- By default the RPC Adapter is configured to return all service invocations as JSON. To see how the RPC Adapter works, invoke the service that you just exposed in a Web browser.
Test the Web application:
- In the Enterprise Explorer view, right-click ShowMovies.jsp, and then select...
Run As | Run on ServerThe Run On Server window opens.
- In the servers list, select WAS v7.0, then click Finish. ShowMovies.jsp opens in a browser. Since you have not populated the Web page, the Web browser displays a blank Web page.
- Once the server is started, click
Window | Show View | Other | General | Internal Web Browser | OK
...to open another Web browser. Paste the following URL into the location bar:
https://localhost:10016/web2Project/RPCAdapter/httprpc/MovieService/getMovieListTo determine the port number for your server, right-click the server in the Servers view and select Properties.
In the Properties list, click...
WAS | The Properties for WebSphere® Application Server v7.0The HTTP port field displays the port number.
- A browser dialog opens asking you how to handle the getMovieList file. Save the file.
- Open the file in a text editor. It looks like the following:
{"result":[{"director":"Victor Fleming","actor":"Vivien Leigh","rating":10,"title":"Gone with the Wind"}, {"director":"Robert Zemeckis","actor":"Michael J Fox", "rating":10,"title":"Back To The Future"},{"director": "George Lucas","actor":"Harrison Ford","rating":10, "title":"Star Wars"}]}The Movie list is serialized as JSON.
Tip: The RPC Adapter includes function that displays all of the services and methods that are currently exposed. With the server running, open a Web browser and type the following URL into the location bar:
http://localhost:<port>/web2Project/RPCAdapter/httprpc/The browser displays the exposed services as a table:
When you click on a service, the individual methods of the service are displayed:
Web-remoting is a pattern that provides support for JavaScript or client-side code to directly invoke server side logic. This pattern provides the ability to invoke Java™ methods from JavaScript. The invocation is by means of a JSON-RPC call. The most common usage is asynchronous calls with XmlHttpRequest. Data is transferred between the server and client in JavaScript Object Notation (JSON) format. Therefore, this pattern is essentially a form of JSON Web services.
IBM implementation for Web remoting is referred to as the Remote Procedure Call (RPC) adapter for IBM. The RPC adapter is designed to help developers create command-based services quickly and easily in a manner that complements programming styles for AJAX applications and other lightweight clients. Implemented as a generic servlet, the RPC adapter provides an HTTP interface to registered JavaBeans.
The RPC adapter provides an HTTP interfaces to registered JavaBeans. It will deserialize the input and call the corresponding method in the JavaBean. It will serialize the output from the JavaBean to JSON/XML format.
The RPC adapter currently supports two RPC protocols:
- HTTP RPC, which encodes RPC invocations as URLs with query parameters, for HTTP GET, or form parameters, for HTTP POST.
- JSON-RPC, supports the SMD service descriptor employed by the Dojo dojo.rpc.JsonService API.
The Remote Procedure Call (RPC) Adapter provides a mechanism for exposing server-side Java objects to AJAX-based user interfaces.
Lesson checkpoint
You have created the RPC Adapter service that returns a JSON response.
You learned the following:
- How to create a Web page.
- How to use the Page Data view to expose the RPC Adapter service.
- About the RCP Adapter as a mechanism for exposing server-side Java objects to AJAX-based user interfaces.