ycycoco 发表于 2017-1-11 07:36:23

架设 apache Axis的webservices服务器和客户端

  1. 服务端
  web服务器: Tomcat5.5, 用到Axis2 1_4_1 (http://apache.mirror.phpchina.com/ws/axis2/1_4_1/
)
  取其下webapps下的axis
文件夹.放到Tomcat /webapps下.
  写一个要发布的的java类 

public class Hello{   
public String sayHello(String name){   
return "你好,"+name;
}   
}
  将该文件(Hello.java)改名为Hello.jws
并拷贝到Tomcat 的 webapps\axis下.
  下面我们就可以测试该Web服务:
  启动Tomcat, 打开浏览器并输入刚刚创建的文件名对应的URL地址 http://localhost:8089/axis/Hello.jws



There is a Web Service here

       

        Click to see the WSDL

  点击可以查看到WSDL文档,如下 
  
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://localhost:8089/axis/Hello.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8089/axis/Hello.jws" xmlns:intf="http://localhost:8089/axis/Hello.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/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="Hello">
<wsdl:operation name="sayHello" parameterOrder="name">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="impl:Hello">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8089/axis/Hello.jws" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port binding="impl:HelloSoapBinding" name="Hello">
<wsdlsoap:address location="http://localhost:8089/axis/Hello.jws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>




  webservices服务端发布完毕.
  ------------------------------------------------------------------------------------------------------------------------------
  webservices客户端

  根据wsdl文件生成java文件

  1. 新建一文件夹用于保存下列文件和文件夹. 
  2. 下载WSDL文档, 保存为xml格式,取名为wsdl.xml

.
  3.   新建一个wsdl2java.bat文件,输入

set Axis_Lib=lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=class
set Package=com.axisws.client
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o %Output_Path% -p %Package% wsdl.xml
  备注: Axis_Lib
设置你的axis相关jar包,可以是绝对路径, 本例jar包放在本目录下的lib文件夹下.

                   Output_Path
为输出java文件的路径,可以为绝对路径, 本例生成到本目录下的class文件夹下.

                   Package
为生成文件的默认包名.

                   最后的wsdl.xml
为你下载的WSDL文档.

  4. 在本目录下的lib文件夹下放入下列jar包(11个, 都在下载的Axis2 1_4_1的lib下)
  activation.jar   axis.jar    axis-ant.jar    commons-discovery-0.2.jar    commons-logging-1.0.4.jar
  jaxrpc.jar     log4j-1.2.8.jar    mailapi_1_3_1.jar    saaj.jar    wsdl4j-1.5.1.jar    xml-apis-2.6.2.jar
  5. 文件夹包含 :wsdl.xml     wsdl2java.bat     lib文件夹

  运行批处理文件后, 会生成一个class文件夹.文件夹内就是生成的类文件.
  Hello_PortType.java               HelloService.java
  HelloServiceLocator.java        HelloSoapBindingStub.java
  6. 测试客户端, 新建java项目, 把生成的文件连包一起导入, lib内的jar包也导入(文件运行需要).
  7. 编写测试文件

package com.axisws.client.test;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.axisws.client.HelloServiceLocator;
import com.axisws.client.Hello_PortType;
public class TestAxisClient {
public static void main(String[] args) {
HelloServiceLocator service = new HelloServiceLocator();
Hello_PortType hello = null;
try {
hello = service.getHello();
System.out.println(hello.sayHello("Tom"));
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}

  运行得到:     你好,Tom
页: [1]
查看完整版本: 架设 apache Axis的webservices服务器和客户端