java axis webservice 开发实例

今天无事看了看webservice,通过网上自己也总结了一下,大概需要以下jar包
activation.jar
axis-ant.jar
axis.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
log4j-1.2.8.jar
mail-1.4.jar
saaj.jar

wsdl4j-1.5.1.jar
首先写deploy.wsdd代码如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="" target="_blank">http://xml.apache.org/axis/wsdd/providers/java">
  <service name="HelloWorld" provider="java:RPC">
    <parameter name="className" value="com.wdg.MyLogin"/>
    <parameter name="allowedMethods" value="*"/>
  </service>
</deployment>
启动tomcat ,运行-cmd  到deploy.wsdd文件所在目录下使用命令
D:\Tomcat 6.0\webapps\mywebservices\WEB-INF>java -Djava.ext.dirs="D:\Tomcat 6.0\webapps\mywebservices\WEB-INF\lib" org.apache.axis.client.AdminClient deploy.wsdd
通过此操作来得到一个server-config.wsdd文件,此操作可以跳过直接找个server-config.wsdd文件改一下即可使用;

server-config.wsdd文件与web.xml放在同级目录下,代码如下:
<?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="sendMultiRefs" value="true"/>
  <parameter name="disablePrettyXML" value="true"/>
  <parameter name="dotNetSoapEncFix" value="true"/>
  <parameter name="enableNamespacePrefixOptimization" value="false"/>
  <parameter name="sendXMLDeclaration" value="true"/>
  <parameter name="sendXsiTypes" value="true"/>
  <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/>
</globalConfiguration>


  <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>  
  <service name="MyLogin.jws" provider="java:RPC">   
     <parameter name="className" value="com.wdg.MyLogin"/>
     <parameter name="scope" value="request"/>
     <parameter name="allowedMethods" value="*"/>
     <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
  </service>   
  <transport name="http">   
     <requestFlow>   
        <handler type="URLMapper"/>   
     </requestFlow>   
  </transport>   
</deployment>

类文件MyLogin
package com.wdg;
public class MyLogin {

public String login(String name,String pwd){
return "用户名:"+name+"\n用户密码:"+pwd;
}
}


web.xml文件增加代码:
<servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

运行url->http://localhost:8080/mywebservices/services

测试类调用service中的方法
// 指出service所在URL
String url = "http://localhost:8080/mywebservices/services/MyLogin.jws";
// 创建一个服务(service)调用(call)
Service service = new Service();
// 通过service创建call对象
Call call = (Call) service.createCall();
// 设置service所在URL
call.setTargetEndpointAddress(new java.net.URL(url));
// 调用service端方法名
call.setOperationName("login");
// Object 数组封装了参数
String strs = (String) call.invoke(new Object[] { "XXX","***" });
System.out.println(strs);

你可能感兴趣的:(webservice,axis,在线应用服务)