1、安装
以tomcat4.0.1为服务器,下面说明如何安装axis:
1.解压下载后的包,将包中axis目录复制到tomcat目录下的webapps目录下;
2.将axis/WEB-INF/lib目录下类文件复制到tomcat目录下的common/lib目录下;
3.重新启动tomcat;
4.访问http://localhost:8080/axis/happyaxis.jsp,如果能访问,表示安装成功;
2、开发webservice服务
a.编写普通类文件,如下所示:
//文件名:Test.java
import java.util.*;
public class Test
{
//fields
private String name="hh";
private int age=20;
//method at here.
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
将本文件(Test.java)复制到Axis目录下,并将其更名为Test.jws;
b.访问http://localhost:8080/axis/Test.jws,Axis就会编译该文件,并将其部署到系统中,下面你就可以开发web service的客户端程序了;
3、开发客户端程序
下面是客户端程序:
import org.apache.axis.client.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
public class TestWebService
{
public static void main(String args[])
{
System.out.println("Start invoking....");
try {
String endpoint =
"http://localhost:8080/axis/Test.jws";
//你写的那个文件
Service service = new Service();
Call call = (Call)
service.createCall();
call.setTargetEndpointAddress
( new java.net.URL(endpoint) );
call.setOperationName("getAge");
//填写你要调用的方法名称
int ret =Integer.parseInt
(("" + call.invoke( new Object[] {} )));
System.out.println(ret);
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Finished the invoking.");
}
}
关于业务调用封装到服务端程序里,然后部署就可以了
使用java调用
需要下载apache soap。下载地址:http://xml.apache.org/soap/index.html
导入:
import org.apache.soap.*;
import org.apache.soap.rpc.*;
例程:
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;
public class testClient {
public static void main(String[] args) throws Exception {
URL url = new URL ("http://192.168.0.4/yundan/service1.wsdl");
//改成你的地址
SOAPMappingRegistry smr = new SOAPMappingRegistry ();
StringDeserializer sd = new StringDeserializer ();
smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName ("", "Result"), null, null, sd);
// 创建传输路径和参数
SOAPHTTPConnection st = new SOAPHTTPConnection();
// 创建调用
Call call = new Call ();
call.setSOAPTransport(st);
call.setSOAPMappingRegistry (smr);
call.setTargetObjectURI ("http://tempuri.org/message/");
call.setMethodName("addNumbers");
call.setEncodingStyleURI ("http://schemas.xmlsoap.org/soap/encoding/");
Vector params = new Vector();
params.addElement(new Parameter("NumberOne", Double.class, "10", null));
params.addElement(new Parameter("NumberTwo", Double.class, "25", null));
call.setParams(params);
Response resp = null;
try {
resp = call.invoke (url, "http://tempuri.org/action/Hello2.addNumbers");
}
catch (SOAPException e) {
System.err.println("Caught SOAPException (" + e.getFaultCode () + "): " + e.getMessage ());
return;
}
// 检查返回值
if (resp != null && !resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object value = ret.getValue();
System.out.println ("Answer--> " + value);
}
else {
Fault fault = resp.getFault ();
System.err.println ("Generated fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
System.out.println (" Fault String = " + fault.getFaultString());
}
}
}
以tomcat4.0.1为服务器,下面说明如何安装axis:
1.解压下载后的包,将包中axis目录复制到tomcat目录下的webapps目录下;
2.将axis/WEB-INF/lib目录下类文件复制到tomcat目录下的common/lib目录下;
3.重新启动tomcat;
4.访问http://localhost:8080/axis/happyaxis.jsp,如果能访问,表示安装成功;
2、开发webservice服务
a.编写普通类文件,如下所示:
//文件名:Test.java
import java.util.*;
public class Test
{
//fields
private String name="hh";
private int age=20;
//method at here.
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
将本文件(Test.java)复制到Axis目录下,并将其更名为Test.jws;
b.访问http://localhost:8080/axis/Test.jws,Axis就会编译该文件,并将其部署到系统中,下面你就可以开发web service的客户端程序了;
3、开发客户端程序
下面是客户端程序:
import org.apache.axis.client.*;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
public class TestWebService
{
public static void main(String args[])
{
System.out.println("Start invoking....");
try {
String endpoint =
"http://localhost:8080/axis/Test.jws";
//你写的那个文件
Service service = new Service();
Call call = (Call)
service.createCall();
call.setTargetEndpointAddress
( new java.net.URL(endpoint) );
call.setOperationName("getAge");
//填写你要调用的方法名称
int ret =Integer.parseInt
(("" + call.invoke( new Object[] {} )));
System.out.println(ret);
} catch (Exception e) {
System.err.println(e.toString());
}
System.out.println("Finished the invoking.");
}
}
关于业务调用封装到服务端程序里,然后部署就可以了
使用java调用
需要下载apache soap。下载地址:http://xml.apache.org/soap/index.html
导入:
import org.apache.soap.*;
import org.apache.soap.rpc.*;
例程:
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;
public class testClient {
public static void main(String[] args) throws Exception {
URL url = new URL ("http://192.168.0.4/yundan/service1.wsdl");
//改成你的地址
SOAPMappingRegistry smr = new SOAPMappingRegistry ();
StringDeserializer sd = new StringDeserializer ();
smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName ("", "Result"), null, null, sd);
// 创建传输路径和参数
SOAPHTTPConnection st = new SOAPHTTPConnection();
// 创建调用
Call call = new Call ();
call.setSOAPTransport(st);
call.setSOAPMappingRegistry (smr);
call.setTargetObjectURI ("http://tempuri.org/message/");
call.setMethodName("addNumbers");
call.setEncodingStyleURI ("http://schemas.xmlsoap.org/soap/encoding/");
Vector params = new Vector();
params.addElement(new Parameter("NumberOne", Double.class, "10", null));
params.addElement(new Parameter("NumberTwo", Double.class, "25", null));
call.setParams(params);
Response resp = null;
try {
resp = call.invoke (url, "http://tempuri.org/action/Hello2.addNumbers");
}
catch (SOAPException e) {
System.err.println("Caught SOAPException (" + e.getFaultCode () + "): " + e.getMessage ());
return;
}
// 检查返回值
if (resp != null && !resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object value = ret.getValue();
System.out.println ("Answer--> " + value);
}
else {
Fault fault = resp.getFault ();
System.err.println ("Generated fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
System.out.println (" Fault String = " + fault.getFaultString());
}
}
}