public class SayHello { public String getName(String name) { return "hello "+name; } }
4、编写访问服务的客户端 SayHelloClient.java
import org.apache.Axis.client.Call; import org.apache.Axis.client.Service; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import java.net.MalformedURLException; import java.rmi.RemoteException; public class SayHelloClient { public static void main(String[] args) { try { String endpoint = "http://localhost:8080/axis/SayHello.jws"; Service service = new Service(); Call call = null; call = (Call) service.createCall(); call.setOperationName(new QName("http://localhost:8080/axis/SayHello.jws", "getName")); call.setTargetEndpointAddress(new java.net.URL(endpoint)); String ret = (String) call.invoke(new Object[] {"someone's name"}); System.out.println("return value is " + ret); } catch (Exception ex) { ex.printStackTrace(); } } }
public interface HelloClientInterface extends java.rmi.Remote { public String getName(String name) throws java.rmi.RemoteException; }
import javax.xml.rpc.Service; import javax.xml.rpc.ServiceFactory; import java.net.URL; import javax.xml.namespace.QName; public class TestHelloClient { public static void main(String[] args) { try { String wsdlUrl = "http://localhost:8080/axis/SayHello.jws?wsdl"; String nameSpaceUri = "http://localhost:8080/axis/SayHello.jws"; String serviceName = "SayHelloService"; String portName = "SayHello"; ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service afService = serviceFactory.createService(new URL(wsdlUrl), new QName(nameSpaceUri, serviceName)); HelloClientInterface proxy = (HelloClientInterface) afService.getPort(new QName(nameSpaceUri, portName), HelloClientInterface.class); System.out.println("return value is "+proxy.getName("someone's name") ) ; } catch(Exception ex) { ex.printStackTrace() ; } } }
package server; public class SayHello { public String getName(String name) { return "hello "+name; } }
import org.apache.axis.AxisFault; import org.apache.axis.Handler; import org.apache.axis.MessageContext; import org.apache.axis.handlers.BasicHandler; import java.util.Date; public class LogHandler extends BasicHandler { public void invoke(MessageContext msgContext) throws AxisFault { /** Log an access each time we get invoked. */ try { Handler serviceHandler = msgContext.getService(); Integer numAccesses = (Integer)serviceHandler.getOption("accesses"); if (numAccesses == null) numAccesses = new Integer(0); numAccesses = new Integer(numAccesses.intValue() + 1); Date date = new Date(); String result = date + ": service " + msgContext.getTargetService() + " accessed " + numAccesses + " time(s)."; serviceHandler.setOption("accesses", numAccesses); System.out.println(result); } catch (Exception e) { throw AxisFault.makeFault(e); } } }
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns= "http://xml.apache.org/Axis/wsdd/" xmlns:java= "http://xml.apache.org/Axis/wsdd/providers/java"> <handler name="print" type="java:LogHandler"/> <service name="SayHello" provider="java:RPC"> <requestFlow> <handler type="print"/> </requestFlow> <parameter name="className" value="server.SayHello"/> <parameter name="allowedMethods" value="*"/> </service> </deployment>
java org.apache.axis.client.AdminClient deploy.wsdd
java org.apache.axis.wsdl.WSDL2Java -p client http://localhost:8080/axis/services/SayHello.jws?wsdl
java org.apache.axis.wsdl.Java2WSDL -oSayHello.wsdl -lhttp://localhost:8080/axis/services/SayHello -nSayHello server.SayHello
java org.apache.axis.wsdl.WSDL2Java SayHello.wsdl -p client
import client.*; public class SayHelloClient { public static void main(String[] args){ try{ SayHelloService service = new client.SayHelloServiceLocator(); client.SayHello_PortType client = service.getSayHello(); String retValue=client.getName("someone's name"); System.out.println(retValue); } catch (Exception e) { System.err.println("Execution failed. Exception: " + e); } } }
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassword" value="admin"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session"/> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request"/> <parameter name="extension" value=".jwr"/> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <handler name="print" type="java:LogHandler"/> <service name="sayhello" provider="java:RPC"> <requestFlow> <handler type="print"/> </requestFlow> <parameter name="className" value="server.SayHello"/> <parameter name="allowedMethods" value="*"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder"/> </responseFlow> </transport> </deployment>