📄 jaxrpc4.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Creating a Web Service with JAX-RPC</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JAXRPC3.html" /> <link rel="Next" href="JAXRPC5.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <table width="550" summary="layout" id="SummaryNotReq1"> <tr> <td align="left" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a> </td> <td align="center" valign="center"><a accesskey="p" href="JAXRPC3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXRPC5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a> </td> <td align="right" valign="center"> <font size="-1"> <a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a> <br> <a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font> </font> </td> </tr> </table> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <blockquote><a name="wp115211"> </a><h2 class="pHeading1">Creating a Web Service with JAX-RPC</h2><a name="wp115205"> </a><p class="pBody">This section shows how to build and deploy a simple Web service called <code class="cCode">MyHelloService</code>. A later section, <a href="JAXRPC5.html#wp79960">Creating Web Service Clients with JAX-RPC</a>, provides examples of JAX-RPC clients that access this service. The source code required by <code class="cCode">MyHelloService</code> is in <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/helloservice</code>/.</p><a name="wp82614"> </a><p class="pBody">These are the basic steps for creating the service:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp82103"> </a><div class="pSmartList1"><li>Code the service endpoint interface and implementation class.</li></div><a name="wp117372"> </a><div class="pSmartList1"><li>Build and generate the files required by the service.</li></div><a name="wp117377"> </a><div class="pSmartList1"><li>Use <code class="cCode">deploytool</code> to package the service's files into a WAR file.</li></div><a name="wp82110"> </a><div class="pSmartList1"><li>Deploy the WAR file.</li></div></ol></div><a name="wp83741"> </a><p class="pBody">The sections that follow cover these steps in greater detail. </p><hr><a name="wp117384"> </a><p class="pNote">Note: Before proceeding, you should try out the examples in the <a href="WebApp.html#wp76431">Getting Started with Web Applications</a> chapter. Make sure that you've followed the instructions in <a href="WebApp3.html#wp213795">Setting Up To Build and Deploy Tutorial Examples</a>.</p><hr><a name="wp80033"> </a><h3 class="pHeading2">Coding the Service Endpoint Interface and Implementation Class</h3><a name="wp80035"> </a><p class="pBody">A service endpoint interface declares the methods that a remote client may invoke on the service. In this example, the interface declares a single method named <code class="cCode">sayHello</code>. </p><a name="wp82143"> </a><p class="pBody">A service endpoint interface must conform to a few rules:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp80036"> </a><div class="pSmartList1"><li>It extends the <code class="cCode">java.rmi.Remote</code> interface.</li></div><a name="wp80037"> </a><div class="pSmartList1"><li>It must not have constant declarations, such as <code class="cCode">public final static</code>.</li></div><a name="wp80038"> </a><div class="pSmartList1"><li>The methods must throw the <code class="cCode">java.rmi.RemoteException</code> or one of its subclasses. (The methods may also throw service-specific exceptions.)</li></div><a name="wp80039"> </a><div class="pSmartList1"><li>Method parameters and return types must be supported JAX-RPC types. See the section <a href="JAXRPC2.html#wp79859">Types Supported By JAX-RPC</a>.</li></div></ul></div><a name="wp80043"> </a><p class="pBody">In this example, the service endpoint interface is <code class="cCode">HelloIF.java</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package helloservice;import java.rmi.Remote;import java.rmi.RemoteException;public interface HelloIF extends Remote { public String sayHello(String s) throws RemoteException;}<a name="wp80044"> </a></pre></div><a name="wp80045"> </a><p class="pBody">In addition to the interface, you'll need the class that implements the interface. In this example, the implementation class is called <code class="cCode">HelloImpl</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">package helloservice;public class HelloImpl implements HelloIF { public String message ="Hello"; public String sayHello(String s) { return message + s; }}<a name="wp80046"> </a></pre></div><a name="wp79980"> </a><h3 class="pHeading2">Building the Service</h3><a name="wp79966"> </a><p class="pBody">To build <code class="cCode">MyHelloService</code>, in a terminal window go to the <code class="cVariable"><INSTALL></code><code class="cCode">/j2eetutorial14/examples/jaxrpc/helloservice/</code> directory and type the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant build<a name="wp117398"> </a></pre></div><a name="wp89558"> </a><p class="pBody">The preceding command executes these <code class="cCode">asant</code> tasks: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp89559"> </a><div class="pSmartList1"><li><code class="cCode">compile-service</code> </li></div><a name="wp117408"> </a><div class="pSmartList1"><li><code class="cCode">generate-wsdl</code> </li></div></ul></div><a name="wp84924"> </a><h4 class="pHeading3">compile-service</h4><a name="wp84926"> </a><p class="pBody">This <code class="cCode">asant</code> task compiles <code class="cCode">HelloIF.java</code> and <code class="cCode">HelloImpl.java</code>, writing the class files to the <code class="cCode">build</code> subdirectory.</p><a name="wp117419"> </a><h4 class="pHeading3">generate-wsdl</h4><a name="wp117420"> </a><p class="pBody">The <code class="cCode">generate-wsdl</code> task runs the <code class="cCode">wscompile</code> tool, which creates the <code class="cCode">MyHelloService.wsdl</code> file and <code class="cCode">mapping.xml</code> file in the <code class="cCode">build</code> directory. The <code class="cCode">generate-wsdl</code> task runs <code class="cCode">wscompile</code> as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">wscompile -define -mapping build/mapping.xml -d build -nd build -classpath build config-interface.xml<a name="wp117421"> </a></pre></div><a name="wp117422"> </a><p class="pBody">The <code class="cCode">-define</code> flag instructs the tool to read the service endpoint interface and to create a WSDL file. The <code class="cCode">-mapping</code> flag specifies the mapping file. The <code class="cCode">-d</code> and <code class="cCode">-nd</code> flags tell the tool to write output to the <code class="cCode">build </code>subdirectory. The tool reads the following <code class="cCode">config-interface.xml </code>file:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version="1.0" encoding="UTF-8"?><configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -