Axis 部署开发WebService

来源:互联网 发布:c 手机编程 编辑:程序博客网 时间:2024/04/24 19:24

   一、Dynamic Proxy方式 (动态代理)

     1、先下载axis-1.4.zip:http://download.csdn.net/detail/jiangqingqingqing/4476589。解压文把axis文件夹复制到Tomcat的webapps目录下面。还需要一些jar包:http://download.csdn.net/detail/jiangqingqingqing/4476520。

     2、创建一个普通的Java Projoct,然后新建一个接口,该接口必须继承java.rmi.Remote。该接口将作为Proxy,代码如下: 

       import java.rmi.Remote;
       public interface HelloWorldInterface
extends Remote{
             String getName(String name);
             int add(int a,int b);
       }

   3、新建一个 class:HelloWorld(不一定得实现HelloWorldInterface接口,但建议最好实现),代码如下:

       public class HelloWorld implements HelloWorldInterface{
       public String getName(String name){
 return "Hello ,"+name;
      }
  
       public int add(int a,int b){
 return a+b;
        }
    }

  4、将HelloWorld.java复制到刚才部署在tomcat的axis文件夹里面,并名为HelloWorld.jws。然后重新启动tomcat,在浏览器中输入http://localhost:端口号/axis/HelloWorld.jws?wsdl,就会得到一个wsdl文件,表明服务发布成功!

   

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/HelloWorld.jws" xmlns:intf="http://localhost:8080/axis/HelloWorld.jws"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchematargetNamespace="http://localhost:8080/axis/HelloWorld.jws">
<!--
WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT)
-->
<wsdl:message name="getNameRequest">
<wsdl:part name="name" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="addRequest">
<wsdl:part name="a" type="xsd:int"/>
<wsdl:part name="b" type="xsd:int"/>
</wsdl:message>
<wsdl:message name="getNameResponse">
<wsdl:part name="getNameReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="addResponse">
<wsdl:part name="addReturn" type="xsd:int"/>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="add" parameterOrder="a b">
<wsdl:input message="impl:addRequest" name="addRequest"/>
<wsdl:output message="impl:addResponse" name="addResponse"/>
</wsdl:operation>
<wsdl:operation name="getName" parameterOrder="name">
<wsdl:input message="impl:getNameRequest" name="getNameRequest"/>
<wsdl:output message="impl:getNameResponse" name="getNameResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="add">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="addRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
</wsdl:input>
<wsdl:output name="addResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloWorld.jws" use="encoded"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getName">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getNameRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
</wsdl:input>
<wsdl:output name="getNameResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/HelloWorld.jws" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldService">
<wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld">
<wsdlsoap:address location="http://localhost:8080/axis/HelloWorld.jws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 5、调用服务

  import java.net.MalformedURLException;
import java.net.URL;


import javax.xml.namespace.QName;
import javax.xml.rpc.*;


public class Text {
   public static void main(String[] args) throws ServiceException, MalformedURLException {
 //服务的路径
 String wsdlURL = "http://localhost:8080/axis/HelloWorld.jws?wsdl";
 //命名空间,必须跟wsdl文件里的targetNamespace标签一致
 String nameSpace = "http://localhost:8080/axis/HelloWorld.jws";
 String serviceName = "HelloWorldService";//必须跟wsdl文件里面的“service” 标签的name属性一致
 String portType = "HelloWorld";//必须跟wsdl文件里面的“portType” 标签的name属性一致
 //创建一个服务工厂
 ServiceFactory factory = ServiceFactory.newInstance();
 //通过服务工厂获取一个服务实例:服务路径,服务的QName
 Service service = factory.createService(new URL(wsdlURL),new QName(nameSpace,serviceName));
 //创建代理
 HelloWorldInterface h = ( HelloWorldInterface)service.getPort(new QName(nameSpace,portType),  HelloWorldInterface.class);
 //通过代理调用方法
 System.out.println(h.getName("Bat_jiang")+" sum = "+h.add(1, 2));
    }
}

6.输出结果

  Hello ,Bat_jiang sum = 3


二、axis2 pojo

1、同样的,先下载axis2.war:http://download.csdn.net/detail/jiangqingqingqing/4476628,然后部署在tomcat上。还有一些jar包:http://download.csdn.net/detail/jiangqingqingqing/4476529

   2、编写一个HelloWorld类(注意:类里不能有package 包名):

    public class HelloWorld {
  public String getName(String name){
 return "Hello ,"+name;
  }
  
  public int add(int a,int b){
 return a+b;
  }
}

3、编译生成HelloWorld.class。然后复制到部署后的axis2\WEB-INF\pojo文件夹里(如果没有pojo文件夹则新建一个)。在浏览器里输入:http://localhost:端口号/axis2/services/listServices,则可以查看到服务列表:

    Back Home  |   Refresh 

Available services

Version

Service EPR : http://localhost:8080/axis2/services/Version

Service Description : Version

Service Status : Active
Available Operations
  • getVersion

TimeTest

Service EPR : http://localhost:8080/axis2/services/TimeTest

Service Description : No description available for this service

Service Status : Active
Available Operations
  • getDate

HelloWorld

Service EPR : http://localhost:8080/axis2/services/HelloWorld

Service Description : No description available for this service

Service Status : Active
Available Operations
  • add
  • getName

4、调用服务

    import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


import com.HelloWorld;
import com.TimeTest;


public class test {
  public static void main(String[] args) throws AxisFault {
//命名空间,必须跟wsdl文件里的targetNamespace标签一致
String nameSpace = "http://ws.apache.org/axis2";
// EndpointReference的前缀,加上具体的类名则可以形成一个 EndpointReference对象
String endPointUrl = "http://localhost:8080/axis2/services";
//用RPC(远程过程调用协议)方法调用
RPCServiceClient service = new  RPCServiceClient();
Options option = service.getOptions();
 
/*
 * 服务1
 */
EndpointReference endpoint = new EndpointReference(endPointUrl+"/HelloWorld");
option.setTo(endpoint); 
//service.invokeBlocking(QName,传入的参数(没有也要是new Object[]{}),返回的类型(没有也要是new Class[]{})
HelloWorld h = (HelloWorld)service.invokeBlocking(new QName(nameSpace,"add"), new Object[]{1,2},new Class[]{HelloWorld.class})[0];
//调用
System.out.println(h.add(1, 2));
System.out.println(h.getName("Bat_jiang"));

 
/*
 * 服务2
 */
endpoint =new EndpointReference(endPointUrl+"/TimeTest");
option.setTo(endpoint);
//调用
TimeTest t = (TimeTest)service.invokeBlocking(new QName(nameSpace,"getDate"), new Object[]{},new Class[]{TimeTest.class})[0];
System.out.println(t.getDate());
 
  }
}

5、运行结果

   3
   Hello ,Bat_jiang
   2012-08-04 08:21:16

原创粉丝点击