webService(Axis1.4)

开发环境:myeclipse6.0+tomcat 5.5+JDK 1.4+Axis1.4

1:首先要先到网站上去下载 Axis1.4 架包,最好下载axis-src-1_4.tar.gz。然后要将这个架包lib下面所有的文件复制到项目lib下面,还要将webapps\axis\WEB-INF\下面的web.xml复制到项目的WEB-INF\下面。

这里主要介绍Axis1.4 的Stubs 方式

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>Apache-Axis</display-name>
    
    <listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>
    
  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <display-name>Apache-Axis Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>AdminServlet</servlet-name>
    <display-name>Axis Admin Servlet</display-name>
    <servlet-class>
        org.apache.axis.transport.http.AdminServlet
    </servlet-class>
    <load-on-startup>100</load-on-startup>
  </servlet>

  <servlet>
    <servlet-name>SOAPMonitorService</servlet-name>
    <display-name>SOAPMonitorService</display-name>
    <servlet-class>
        org.apache.axis.monitor.SOAPMonitorService
    </servlet-class>
    <init-param>
      <param-name>SOAPMonitorPort</param-name>
      <param-value>5001</param-value>
    </init-param>
    <load-on-startup>100</load-on-startup>
  </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-mapping>
    <servlet-name>SOAPMonitorService</servlet-name>
    <url-pattern>/SOAPMonitor</url-pattern>
  </servlet-mapping>

 <!-- uncomment this if you want the admin servlet -->
 <!--
  <servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
  </servlet-mapping>
 -->

    <session-config>
        <!-- Default to 5 minute session timeouts -->
        <session-timeout>5</session-timeout>
    </session-config>

    <!-- currently the W3C havent settled on a media type for WSDL;
    http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
    for now we go with the basic 'it's XML' response -->
  <mime-mapping>
    <extension>wsdl</extension>
     <mime-type>text/xml</mime-type>
  </mime-mapping>
  

  <mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
  </mime-mapping>

  <welcome-file-list id="WelcomeFileList">
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jws</welcome-file>
  </welcome-file-list>

</web-app>

 以上这个是web.xml文件

2:接着 在项目的WEB-INF下面配置一个server-config.wsdd。下面是WSDD文件的配置

<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>   
   <service name="axisTest" provider="java:RPC">
        <parameter name="className" value="cn.com.self.axisWebservice.axisTest"/>//类的路径
        <parameter name="allowedMethods" value="*"/>
    </service>
<transport name="http">
<requestFlow>
    <handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>

 3:下面我们写一个.java 文件

package cn.com.self.axisWebservice;

public class axisTest {
	public String Hello(String name){
		return "欢迎使用axisWebservice"+name;
	}
}

4:在项目的根目录下建一个java2wsdl文件夹,然后在其下面创建一个build.xml文件,具体内容如下

<?xml version="1.0" encoding="UTF-8"?>
<project name="Generate WSDL from JavaBeans as Web Services" default="j2w-all" basedir=".">
    <property name="build.dir" value="../../WebRoot/WEB-INF/classes"/>
      <property name="axis.dir" location="D:/axis/axis-1_4"/>
      <path id="classpath.id">
             <fileset dir="${axis.dir}/lib">
                    <include name="*.jar"/>
             </fileset>   
    <pathelement location="${build.dir}"/>
      </path>
<taskdef name="axis-java2wsdl" classname="org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask"
    loaderref="axis" >
        <classpath refid="classpath.id"/>
</taskdef>
       <target name="j2w-all">
          <antcall target="j2w-JavaBeanWS"/>       
       </target>
    <target name="j2w-JavaBeanWS">
        <axis-java2wsdl    classname="cn.com.self.axisWebservice.axisTest" classpath="${build.dir}"
                    methods="Hello"
                        output="axisTest.wsdl"
                        location="http://localhost:8080/WS-self/services/axisTest"
                        namespace="http://localhost:8080/WS-self/services/axisTest"
                        namespaceImpl="http://localhost:8080/WS-self/services/axisTest">        
          </axis-java2wsdl>
    </target>
</project>

 然后用ant构建,则会生成一个AxisTest.wsdl文件

5:在项目的上新建一个源文件夹(注意不是文件夹),然后里面同样也创建一个build.xml。具体内容如下

<?xml version="1.0" encoding="UTF-8"?>
<project name="wsclient" default="all" basedir=".">
<property name="axis.home" location="D:/axis/axis-1_4"/>
<property name="options.output" location="../wsdl2java"/>
<path id="axis.classpath">
    <fileset dir="${axis.home}/lib">
      <include name="**/*.jar"/>
    </fileset>
</path>
<taskdef resource="axis-tasks.properties" classpathref="axis.classpath" />
<target name="-WSDL2Axis" depends="init">
    <mkdir dir="${options.output}"/>
    <axis-wsdl2java output="${options.output}" url="${options.WSDL-URI}" verbose="true"/>
</target>
<target name="init">
    <echo>Warning: please update the associated WSDL file(s) in the folder wsdl before running the target!</echo>
    <echo>Warning: Just run the target(s) related with your developing work!</echo>
    <echo></echo>
</target>
<target name="all">
        <antcall target="axisTest"/>        
</target>
<target name="axisTest">
    <antcall target="-WSDL2Axis">
      <param name="options.WSDL-URI" location="../src/java2wsdl/axisTest.wsdl"/>
    </antcall>
</target>         
</project>

 然后ant构建会生成我们所需要的文件。

6:新建一个类 main方法来调用,如下:

AxisTestServiceLocator Service= new AxisTestServiceLocator();
		AxisTest port = Service.getaxisTest();
		String str=port.hello("张三");
		System.out.print(str);

 

然后 运行如果后台输出“欢迎使用axiswebservice张三” 就表示成功了

你可能感兴趣的:(webservice)