public class HelloWorld
{
public String getName(String name)
{
return "hello "+name;
}
}
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
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(
"http://localhost:8080/Axis
"getName"));
call.setTargetEndpointAddress
(new java.net.URL(endpoint));
String ret = (String) call.invoke(new Object[]
{"zhangsan"});
System.out.println("return value is " + ret);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public interface HelloWorldInterface extends java.rmi.Remote
{
public String getName(String name)
throws java.rmi.RemoteException;
}
3. 编写并执行客户端程序TestHelloClient.java
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
String nameSpaceUri =
"http://localhost:8080/Axis
String serviceName = "HelloWorldService";
String portName = "HelloWorld";
ServiceFactory serviceFactory =
ServiceFactory.newInstance();
Service afService =
serviceFactory.createService(new URL(wsdlUrl),
new QName(nameSpaceUri, serviceName));
HelloWorldInterface proxy = (HelloWorldInterface )
afService.getPort(new QName(
nameSpaceUri, portName),
HelloWorldInterface .class);
System.out.println
("return value is "+proxy.getName("john") ) ;
}catch(Exception ex)
{
ex.printStackTrace() ;
}
}
}
C. 编写wsdd发布web服务,编写stub client访问web服务
1、编写服务端程序SayHello.java,编译SayHello.java
public class SayHello
{
public String getName(String name)
{
return "hello "+name;
}
}
2. 编写LogHandler.java
import org.apache.axis.AxisFault;
import org.apache.axis.Handler;
import org.apache.axis.MessageContext;
import org.apache.axis.handlers
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
if (numAccesses == null)
numAccesses = new Integer(0);
numAccesses = new Integer(numAccesses.intValue() + 1);
Date date = new Date();
serviceHandler.setOption("accesses", numAccesses);
System.out.println(result);
} catch (Exception e)
{
throw AxisFault.makeFault(e);
}
}
}
3. 编写deploy.wsdd文件
<deployment xmlns="http://xml.apache.org/axis
<handler name="print" type="java:LogHandler"/>
<service name="sayhello" provider="java:RPC">
<requestFlow>
<handler type="print"/>
</requestFlow>
<parameter name="className" value="SayHello"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
4. 将编译后的文件拷贝到Axis_HOME/WEB-INF
5. 发布服务,生成服务配置文件 java -cp %AXISCLASSPATH% org.apache.axis.client.AdminCli
6.生成客户端代码
a:方式1
将SayHello.java拷贝到Axis_HOME/下,重命名为SayHello.jws,
执行下面的命令生存client stub
java -cp %AXISCLASSPATH% org.apache.axis.wsdl.WSDL2Java -p client http://localhost:8080/axis
这种方式相当于实际上使用了第一种发布方式来生成 wsdl 文件 然后用WSDL2Java 生成客户端stub代码
b:方式2
这种方式 先用SayHello生成一个静态的SayHello.wsdl 文件,然后用WSDL2Java 生成对应的客户端stub框架
执行如下命令生成SayHello.wsdl
java -cp %AXISCLASSPATH% org.apache.axis.wsdl.Java2WSDL -oSayHello.wsdl -lhttp://localhost:8080/axis
执行如下命令生成client stub
java -cp %AXISCLASSPATH% org.apache.axis.wsdl.WSDL2Java SayHello.wsdl -p client
生成的stub client文件列表为:
1.SayHello.java
2.SayHelloService.java。
3.SayHelloServiceLocator.java
4.SayHelloSoapBindingStub.java
6、编写客户端程序,编译并执行
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("Your name");
System.out.println(retValue);
}
catch (Exception e)
{
System.err.println
("Execution failed. Exception: " + e);
}
}
}
Axis2 的安装配置以及简单例子
String name = param4.getEchoStringParam();
String retStr = "Hello " + name + "!";
EchoStringReturnDocument esrd = EchoStringReturnDocument
esrd.setEchoStringReturn(retStr);
return esrd;
java.lang.String str = new java.lang.String();
Axis2SampleD
//Create the request document to be sent.
EchoStringParamDocument reqDoc= EchoStringParamDocument
reqDoc.setEchoStringParam("Your name");
//invokes the Web service.
EchoStringReturnDocument resDoc = stub.echoString(reqDoc);
System.out.println(resDoc.getEchoStringReturn());
e.printStackTrace();
}
}