Axis2发布webservice

一.axis的定义可在网上查清楚,网上有不同的发布方式。
二.Axis 支持三种web service的部署和开发,分别为:
  1、Dynamic Invocation Interface ( DII)
  2、Dynamic Proxy方式
  3、Stubs方式
三.公司用的第二种方法,所以研究了一下,分享出来
       jar包如下:axis.jar
                         commons-logging.jar
                         commons-discovery-0.2.jar
                         jaxrpc.jar
                         wsdl4j-1.6.1.jar
                         xml-apis-1.0.b2.jar
                         activation-1.1.jar
                         mail-1.4.jar
      新建web project项目如下:
     
 
      在web.xml中配置如下信息:
      <servlet>
  <display-name>Apache-Axis Servlet</display-name>
  <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>/servlet/AxisServlet</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>AxisServlet</servlet-name>
  <url-pattern>*.jws</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>AxisServlet</servlet-name>
  <url-pattern>/services/*</url-pattern>
 </servlet-mapping>
 <servlet>
  <display-name>Axis Admin Servlet</display-name>
  <servlet-name>AdminServlet</servlet-name>
  <servlet-class>
   org.apache.axis.transport.http.AdminServlet
  </servlet-class>
  <load-on-startup>100</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>AdminServlet</servlet-name>
  <url-pattern>/servlet/AdminServlet</url-pattern>
 </servlet-mapping>
    在WEB-INF目录下面建server-config.wsdd文件,文件内容如下:

<ns1:deployment xmlns="http://xml.apache.org/axis/wsdd/"
 xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
 xmlns:ns1="http://xml.apache.org/axis/wsdd/">
 <ns1:globalConfiguration>
  <ns1:parameter name="sendMultiRefs" value="true" />
  <ns1:parameter name="disablePrettyXML" value="true" />
  <ns1:parameter name="adminPassword" value="admin" />  
  <ns1:parameter name="dotNetSoapEncFix" value="true" />
  <ns1:parameter name="enableNamespacePrefixOptimization"
   value="false" />
  <ns1:parameter name="sendXMLDeclaration" value="true" />
  <ns1:parameter name="attachments.implementation"
   value="org.apache.axis.attachments.AttachmentsImpl" />
  <ns1:parameter name="sendXsiTypes" value="true" />
  <ns1:requestFlow>
   <ns1:handler
    type="java:org.apache.axis.handlers.JWSHandler">
    <ns1:parameter name="scope" value="session" />
   </ns1:handler>
   <ns1:handler
    type="java:org.apache.axis.handlers.JWSHandler">
    <ns1:parameter name="scope" value="request" />
    <ns1:parameter name="extension" value=".jwr" />
   </ns1:handler>
  </ns1:requestFlow>
 </ns1:globalConfiguration>
 <ns1:handler name="URLMapper"
  type="java:org.apache.axis.handlers.http.URLMapper" />
 <ns1:handler name="LocalResponder"
  type="java:org.apache.axis.transport.local.LocalResponder" />
 <ns1:handler name="Authenticate"
  type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
 <!-- 发布项目 -->
 <ns1:service name="myService" provider="java:RPC">
  <ns1:parameter name="allowedMethods" value="getUserName" />
  <ns1:parameter name="className"
   value="com.sk.service.MyService" />
 </ns1:service>

 <ns1:service name="AdminService" provider="java:MSG">
  <ns1:parameter name="allowedMethods" value="AdminService" />
  <ns1:parameter name="enableRemoteAdmin" value="false" />
  <ns1:parameter name="className"
   value="org.apache.axis.utils.Admin" />
  <ns1:namespace>http://xml.apache.org/axis/wsdd/</ns1:namespace>
 </ns1:service>

 <ns1:service name="Version" provider="java:RPC">
  <ns1:parameter name="allowedMethods" value="getVersion" />
  <ns1:parameter name="className" value="org.apache.axis.Version" />
 </ns1:service>

 <ns1:transport name="http">
  <ns1:requestFlow>
   <ns1:handler type="URLMapper" />
   <ns1:handler
    type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />
  </ns1:requestFlow>
  <ns1:parameter name="qs:list"
   value="org.apache.axis.transport.http.QSListHandler" />
  <ns1:parameter name="qs:wsdl"
   value="org.apache.axis.transport.http.QSWSDLHandler" />
  <ns1:parameter name="qs.list"
   value="org.apache.axis.transport.http.QSListHandler" />
  <ns1:parameter name="qs.method"
   value="org.apache.axis.transport.http.QSMethodHandler" />
  <ns1:parameter name="qs:method"
   value="org.apache.axis.transport.http.QSMethodHandler" />
  <ns1:parameter name="qs.wsdl"
   value="org.apache.axis.transport.http.QSWSDLHandler" />
 </ns1:transport>
 <ns1:transport name="local">
  <ns1:responseFlow>
   <ns1:handler type="LocalResponder" />
  </ns1:responseFlow>
 </ns1:transport>
</ns1:deployment>

      新建包com.sk.service,新建MyService类,内容如下:
     public String getUserName(String name){
                return "学生姓名是:" + name;
    }
把项目发布到tomcat中,启动tomcat。输入 http://localhost:8080/AxisTest2/servlet/AxisServlet
如下图:
点击myService(wsdl),出现如下:
表示启动成功。
客户端代码如下:
public static void main(String[] args) {
  String URL = " http://localhost:8080/AxisTest/services/myService?wsdl";
  try {
   Service service = new Service();//创建一个service实例,必须要
   Call call = (Call) service.createCall();//创建call实例,必须要
   call.setTargetEndpointAddress(new java.net.URL(URL));//为call设置服务的位置
   call.setOperationName("getUserName");//访问的方法名
   String res = (String)call.invoke(new Object[]{"张三"});//返回String,传入参数
   System.out.println(res);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
运行客户端,控制台打出:学生姓名是:张三
 
目前公司是按照这种方式来实现的。待我去客户端进行联调的时候,还会继续更新。
 
爱生活,爱分享,爱康宝!
 
 
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页