Axis鏀寔涓夌web service鐨勯儴緗插拰寮鍙戯紝鍒嗗埆涓猴細
1銆丏ynamic Invocation Interface ( DII)
2銆丼tubs鏂瑰紡
3銆丏ynamic Proxy鏂瑰紡
浜屻佺紪鍐橠II(Dynamic Invocation Interface )鏂瑰紡web鏈嶅姟
1.緙栧啓鏈嶅姟绔▼搴廐elloClient
public class HelloClient
{
public String getName(String name)
{
return "hello "+name;
}
}
2銆佸皢婧愮爜鎷瘋礉鍒癆xis_HOME涓嬶紝閲嶅懡鍚嶄負 HelloClient.jws
3銆佽闂繛鎺ttp://localhost:8080/Axis/HelloClient.jws?wsdl錛岄〉闈㈡樉紺篈xis鑷姩鐢熸垚鐨剋sdl
4銆佺紪鍐欒闂湇鍔$殑瀹㈡埛绔? TestHelloClient.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 SayHelloClient2
{
public static void main(String[] args)
{
try
{
String endpoint =
"http://localhost:8080/Axis/HelloClient.jws";
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
call.setOperationName(new QName(
"http://localhost:8080/Axis/HelloClient.jws",
"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();
}
}
}
涓夈佺紪鍐橠ynamic Proxy鏂瑰紡璁塊棶鏈嶅姟
1銆佺紪鍐欓儴緗叉湇鍔$紼嬪簭錛屽悓涓婅竟DII鏂瑰紡錛屾湰嬈′粛浣跨敤涓婅竟閮ㄧ講鐨凥elloClient
2銆佺紪鍐欎唬鐞嗘帴鍙?
public interface HelloClientInterface
extends java.rmi.Remote
{
public String getName(String name)
throws java.rmi.RemoteException;
}
3銆佺紪鍐欏茍鎵ц瀹㈡埛绔▼搴廡estHelloClient.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/HelloClient.jws?wsdl";
String nameSpaceUri =
"http://localhost:8080/Axis/HelloClient.jws";
String serviceName = "HelloClientService";
String portName = "HelloClient";
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("john") ) ;
}catch(Exception ex)
{
ex.printStackTrace() ;
}
}
}
鍥涖佺紪鍐檞sdd鍙戝竷web鏈嶅姟錛岀紪鍐檚tub client璁塊棶web鏈嶅姟
1銆佺紪鍐欐湇鍔$紼嬪簭server,SayHello.java錛岀紪璇憇erver.SayHello.java
package server;
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.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);
}
}
}
3銆佺紪鍐檞sdd鏂囦歡
deploy.wsdd
<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>
3銆佸皢緙栬瘧鍚庣殑鏂囦歡鎷瘋礉鍒癆xis_HOME/WEB-INF/classes涓嬶紝濡傦細D:\tomcat\webapps\Axis\WEB-INF\classes
4銆佸彂甯冩湇鍔★細
java org.apache.Axis.client.AdminClient deploy.wsdd
5銆佺敓鎴恈lient stub鏂囦歡
a:鏂瑰紡1
灝哠ayHello.java鎷瘋礉鍒癆xis_HOME/涓嬶紝閲嶅懡鍚嶄負SayHello.jws錛?
鎵ц涓嬮潰鐨勫懡浠ょ敓瀛榗lient stub
java org.apache.Axis.wsdl.WSDL2Java
-p client http://localhost:8080
/Axis/services/SayHello.jws?wsdl
b:鏂瑰紡2
鎵ц濡備笅鍛戒護鐢熸垚SayHello.wsdl
java org.apache.Axis.wsdl.Java2WSDL
-oSayHello.wsdl -lhttp://localhost:8080
/Axis/services/SayHello -nsayhello server.SayHello
鎵ц濡備笅鍛戒護鐢熸垚client stub
java org.apache.Axis.wsdl.WSDL2Java
SayHello.wsdl -p client
鐢熸垚鐨剆tub 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("zhangsan");
System.out.println(retValue);
}
catch (Exception e)
{
System.err.println
("Execution failed. Exception: " + e);
}
}
}