WebService基础学习笔记 - 2.WebService之AXIS定制发布
发布WSDD
1只有class,包含包的结构;
实现步骤:
1编写带有包结构的java代码(普通:方法的编写)
2将该程序进行编译 –class;
3将编译后的结构;
4使用命令在axis服务平台上注册服务(声明服务的方法);
Axis\WEB-INF\
1编写一个当前的项目配置文件server-config.wsdd;
1编写一个*.wsdd文件,描述某一个web服务;
<deployment name=”test” xmlns=http://xml.apache.org/axis/wsdd
Xmlns:java=http://xml.apache.org/axis/wsdd/providers/java>
<service name=”urn:cominfo” provider=”java:RPC”>
<parametername=”className” value=”指明需要发布的类的绝对位置” />
<parametername=”className” value=”方法名(*)”/>
<service>
</deployment>
2 在Axis\web-inf\下面执行命令:
java org.apache.axis.client.AdminClient *.wsdd : axis.jar中类 –CLASSPATH
3 将编译后的结果(包含包的层次)拷贝到Axis\WEB-INF\classes下;
4 使用命令在axis服务平台上注册服务(声明服务的方法)
5 文件的结构
AXIS
WEB-INF
----server-config.wsdd
classes
----包结构的java服务类
6 修改server-config.wsdd
7 取消服务:
1编写取消的*wsdd
<undeploymentname=”test” xmlns=http://xml.apache.org/axis/wsdd/
<service name=”要取消的服务名” />
</undeployment>
2执行命令
Javaorg.apache.axis.client.AdminClient 取消.wsdd;
8. Web Service Object的生存的范围
<parametername=”scope” value=”request/session/application” />
request:Axis为每一个SOAP的请求产生服务对象 ----针对于请求;
session:Axis为每一个调用webService的客户端产生服务对象 ---- 针对会话;
application:在服务器的内存中直接new一个服务对象;
以下是练习:
服务端编写:
package com.webservice.wsdd; public class HelloWSDD {
//定制提供web服务的方法 private int requestCount = 0; public String hello(String name){ requestCount++; System.out.println("调用的次数"+requestCount); return "欢迎您:" + name; }
public Float add(Float a,Float b){ return a+b; }
public static void main(String[] args) { new HelloWSDD(); } } |
编译服务端代码:
将服务端的类,编译成class文件,然后连报名一起复制到Axis\WEB-INF\classes目录下;
编写server-config.wsdd文件:
全部内容如下:
<?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"/>
<service name="HelloWSDD" provider="java:RPC"> <parameter name="className" value="com.webservice.wsdd.HelloWSDD"/> <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> |
客户端调用编写:
package com.client;
import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service;
public class TestClient { public static void main(String[] args) throws ServiceException, RemoteException { String url = "http://localhost:8080/axis/services/HelloWSDD"; /*区别于JWS方式的指定 hptt://localhost:8080/axis/services/HelloWSDD.jws */ Service service = new Service(); Call call = (Call)service.createCall(); call.setTargetEndpointAddress(url); call.setOperationName(new QName(url, "add")); //解析wsdl文件 Float returnValue = (Float) call.invoke(new Object[]{ new Float(3.2), new Float(2.8)}); System.out.println(returnValue); } } |