首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

java axis兑现webservice访问

2012-12-26 
java axis实现webservice访问1、新建一个Web工程,工程名为“AxisTest”。2、把主要jar包拷进lib包中,axis.jar,c

java axis实现webservice访问
1、新建一个Web工程,工程名为“AxisTest”。
2、把主要jar包拷进lib包中,axis.jar,commons-discovery-0.2.jar,commons-logging-1.0.4.jar,jaxrpc.jar,wsdl4j-1.5.1.jar,saaj.jar;可选包(发布服务及生成客户端程序是要用到的):activation.jar;mail.jar。
3、配置“web.xml”:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
    <display-name>Apache-Axis</display-name>   
    <listener>
        <listener-class>org.apache.axis.transport.http.AxisHTTPSessionListener</listener-class>
    </listener>
   
  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>
        org.apache.axis.transport.http.AxisServlet
    </servlet-class>
  </servlet>

  <servlet>
    <servlet-name>AdminServlet</servlet-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>
    <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>
        <session-timeout>20</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>
可参照“axis-1_4\webapps\axis\WEB-INF\web.xml”进行配置;
4、编写服务端程序server,SayHello.java,编译server.SayHello.java
package server;
public class SayHello
{
    public String getName(String name)
    {
        return "hello "+name;
    }
}
5、编写wsdd文件
deploy.wsdd(发布)文件内容如下:
<deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java=
"http://xml.apache.org/axis/wsdd/providers/java">
<service name="SayHello" provider="java:RPC">
    <parameter name="className" value="server.SayHello.getName"/>
<!--allowedMethods value等于*,表示SayHello的所有方法都可以被调用。-->

    <parameter name="allowedMethods" value="*"/>
<!-- request, session, or application -->
<parameter name="scope" value="session"/> </service>
</deployment>
附:undeploy.wsdd(撤销)文件内容
<undeployment xmlns="http://xml.apache.org/axis/wsdd/">
  <service name="SayHello"/>
</undeployment>
6、把工程发布到Tomcat并启动Tomcat;
7、发布服务
编辑一个deploy.bat,Axis_Lib为axis.jar路径。内容如下:
set Axis_Lib=.\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Axis_Servlet=http://localhost:8080/AxisTest/servlet/AxisServlet
%Java_Cmd% org.apache.axis.client.AdminClient -l%Axis_Servlet% deploy.wsdd

执行这个批处理文件,这时候,如果提示成功的话,访问http://localhost:8080/AxisTest/servlet/AxisServlet或http://localhost:8080/AxisTest/services就会显示服务列表

下面来解释AdminClient 所做的工作。AdminClient 首先会在axis.jar所在应用下的WEB-INF文件夹下创建一个webservice配置文件server-config.wsdd,并且根据wsdd文件的配置将我们的webservice加入到这个配置文件中。如果server-config.wsdd已经存在则不会创建,会修改这个文件将新的service 配置到server-config.wsdd文件中。

8、生成客户端client stub文件
在浏览器上访问服务器端的服务,可以下载到WSDL文件,通过Axis的相关工具,可以自动从WSDL文件中生成Web Service的客户端代码。
编写一个WSDL2Java.bat文件,其内容如下:
set Axis_Lib=.\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=.\src
set Package=server.com
set wsdl_path=http://localhost:8080/AxisTest/services/SayHello?wsdl
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% %wsdl_path%

执行这个批处理文件就可以生成client stub.
  生成的stub client文件列表为:SayHello.java,SayHelloService.java,SayHelloServiceLocator.java,SayHelloSoapBindingStub.java .

9、编写客户端程序,编译并执行
1)、Stubs方式
下面是一段junit测试客户端代码。
import java.net.URL;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestWSClient extends TestCase {
    public TestWSClient(String string) {
        super(string);
    }
    public void SayHelloClient() throws Exception {
        SayHelloService service = new SayHelloServiceLocator();
        SayHello_PortType client = service.getSayHello() ;
        String retValue = client.getName("clientname");
        System.out.println(retValue);
    }
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTest(new TestWSClient("SayHelloClient"));
        return suite;
    }
}

2)、动态调用方式:
import javax.xml.namespace.QName;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;

public static void main(String args[]){
try {
           // Options options = new Options(args);
           
            String endpointURL = "http://localhost:8080/AxisTest/services/SayHello";
           
            Service  service = new Service();
            Call     call    = (Call) service.createCall();
            call.setTargetEndpointAddress( new java.net.URL(endpointURL) );
            call.setOperationName( new QName("SayHello", "getName") );
            String res = (String) call.invoke( new Object[] {"Jack"} );
            System.out.println( res );
        } catch (Exception e) {
            System.err.println(e.toString());
        }
}

热点排行