AXIS简单实现入门

类别:Java 点击:0 评论:0 推荐:

http://localhost/services

http://localhost/servlet/AdminServlet

 

l         至 http://ws.apache.org/axis

l         下载解压*.jar至lib

l         修改web.xml如下:

 

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

<!DOCTYPE web-app

    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>

  <display-name>Apache-Axis</display-name>

  <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>8100</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>

 

    <!-- 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.html</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>index.jws</welcome-file>

  </welcome-file-list>

 

l         直接把所要发布的接口,以*.java 放入*.jws入root即ok

 

View the list of deployed Web services

http://localhost/servlet/AxisServlet

 

对*.jws访问进行url监控时我们可以很好的查看对方服务器的内部地址:

http://localhost/UNEXIST.jws 这样只能返回http 404错误

 

http://localhost/UNEXIST.jws?wsdl

AXIS error

Sorry, something seems to have gone wrong... here are the details:

Fault - ; nested exception is:

         java.io.FileNotFoundException: h:\WSDL\defaultroot\xxx.jws (The system cannot find the file specified)

AxisFault

 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException

 faultSubcode:

 faultString: java.io.FileNotFoundException: h:\WSDL\defaultroot\xxx.jws (The system cannot find the file specified)

 faultActor:

 faultNode:

 faultDetail:

 

l         实际编写Test.jws 到root目录

public class Test{

        public String getMsg(){

                System.out.println("happen act in getMsg()");

                return "demo";

        }

        public void setMsg(String msg){

                //--operation here

        }

}

 

while compiling:  h:\WSDL\defaultroot\WEB-INF\jwsClasses\Test.java

l         编写*.jws注意问题: 不能implement XXInterface,不要写package

 

看到效果:

http://localhost/Test.jws

表示正常,关键要WSDL能正常解析出来http://localhost/Test.jws?wsdl

 

There is a Web Service here Click to see the WSDL

 

到此步应该成功,在开始配置时有非常问题,发生在Web.xml与解析WSDL的相关正确经过几次三番成功用的是

JDK1.4+Tomcat4/WebLogic 7+Win

JDK1.3+WebLogic7+Unix tru64 5.1a

问题:

WebLogic7.1有可以,有机不行,一样建domain,总有时就不行

WebLogic7.1在Unix建domain一样copy to DefaultWebApp 就是有时不行,

 

根据WebServices调用之几种方法:

l         http://localhost/Test.jws?method=getMsg

l         http://localhost/Test.jws?method=setMsg&msg=hello

返回整个SOAP包与结果

 

l         JBuilder8->New->Web Services->Import a Web Service:

Import A Web Service With Axis:

WSDL URL: http://localhost/Test.jws?wsdl

package: com.xxxx

生成如下类:

TestService.java

TestServiceLocator.java

TestServiceTestCase.java

Test.java

TestSoapBindingStub.java

TestSoapBindingImpl.java

还有:deploy.wsdd,undeploy.wsdd

 

调用:

TestService ts=new TestServiceLocator();

((TestSoapBindingStub)ts.get....).get..

 

该类为调用基于Microsoft SOAP的解决VC传指针引用返回内容Call远程方法后可调用StringHolder.value 取得值  javax.xml.rpc.holders.StringHolder;

 

 

l         第二种方式,用WSDL2JAVA生成本地调用

//在classpath包含6个jar文件

wsdl2java http://localhost/Test.jws?wsdl

 

F:\axis-1_1> set classpath=f:axis-ant.jar;f:axis.jar;f:commons-discovery.jar;f:co

mmons-logging.jar;f:jaxrpc.jar;f:log4j-1.2.8.jar;f:saaj.jar;f:wsdl4j.jar;

 

d:/j2sdk1.4.0/bin/java org.apache.axis.wsdl.WSDL2Java http://localho

st/Test.jws?wsdl

 

生成四个文件

TestService.java , TestServiceLocator.java , Test.java , TestSoapBindingStub.java

 

TestService test=new TestServiceLocator();

TestSoapBindingStub bind=(TestSoapBindingStub) test.getTestSoapPort();

 

后与jbuilder做本地调用

 

 

l         第三种方式,直接下载www.apache.org/soap 把jar包放入classpath中,编写调用代码:

import java.net.URL;

import java.util.Vector;

import java.util.Arrays;

 

import org.apache.soap.Constants;

import org.apache.soap.SOAPException;

import org.apache.soap.Envelope;

import org.apache.soap.Fault;

import org.apache.soap.rpc.Call;

import org.apache.soap.rpc.Response;

import org.apache.soap.rpc.Parameter;

 

public class HelloWorldClient {

  public static void main(String args[]) {

    String url = "http://localhost/Test.jws";

    String remoteMethod = "setMsg";

 

       String callers[]=new String[]{"Hello"};

    try {

      Call call = new Call();

      call.setMethodName(remoteMethod);

     

      Parameter params[]=new Parameter[callers.length];

      for(int i=0;i<params.length;i++){

          params[i]=new Parameter("callers"+i,callers[i].getClass(),callers[i],

                                             Constants.NS_URI_SOAP_ENC);

      }

 

      Vector vect=new Vector(Arrays.asList(params));     call.setParams(vect);

      Response resp = call.invoke(new URL(url), "");

 

      if (resp.generatedFault()) {

        Fault fault = resp.getFault();

        System.out.println(": Call to " + remoteMethod + " returned a fault!");

        System.out.println("  Fault code: " + fault.getFaultCode());

        System.out.println("  Fault string: " + fault.getFaultString());

      } else {

        if (resp.getReturnValue() != null) {

          Object result = resp.getReturnValue().getValue();

          System.out.println("return" + ": " + result);

        }

      }

    } catch (Exception exception) {

      System.err.println("Error, caught exception: " + exception);

    }

  }

}

本文地址:http://com.8s8s.com/it/it15476.htm