webservice发布服务:AXIS2及客户端调用

1.Axis2:

到官网下载axis2的压缩包。

解压后:

1.将lib文件下的jar包复制到项目中

2.在web-inf下创建services->META-INF->services.xml,并将压缩包中的services.list、version-1.7.4.aar复制到services文件夹下

3.编写services.xml文件,配置发布的接口,格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<service name="testService" targetName="http://cn.itcast.test">
<parameter name="ServiceClass">cn.itcast.test.Test</parameter>
<operation name="add">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="aaa">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<!-- <operation name="saySorry">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getWorld">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation> -->
</service> 4.将编写好的services.xml文件替换version-1.7.4.arr中的services.xml文件(可以将version-1.7.4.arr修改后缀为zip然后打开找到services.xml文件并替换) 5.web.xml文件中配置axis2的servlet过滤器,格式如下: <?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Apache-Axis2</display-name>
<servlet>
<display-name>Apache-Axis Servlet</display-name>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</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>AxisAdminServlet</servlet-name>
<url-pattern>/axis2-admin/*</url-pattern>
</servlet-mapping> <mime-mapping>
<extension>inc</extension>
<mime-type>text/plain</mime-type>
</mime-mapping>
-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>/axis2-web/index.jsp</welcome-file>
</welcome-file-list> <error-page>
<error-code>404</error-code>
<location>/axis2-web/Error/error404.jsp</location>
</error-page> <error-page>
<error-code>500</error-code>
<location>/axis2-web/Error/error500.jsp</location>
</error-page>
</web-app> 启动服务器,访问接口的wsdl文档:http://localhost:8080/axis2Server/services/testService?wsdl,如果能访问到网页版xml格式文档,说明接口发布成功。 客户端生成代码及调用: 去官网下载axis2-1.7.4-bin里面有bin文件,bin文件下有各种以.bat为后缀的命令文件,新建一个自定义.bat文件,一般起名为generateCode.bat(中文:生成代码),文件中的内容如下: wsdl2java -uri http://localhost:8080/axis2Server/services/testService?wsdl -p axis2.service.server -o e:\cn; 上面语句的意思是:axis2.service.server是目录结构;e:\cn是生成在e盘的cn文件夹下。 然而上面的wsdl2java命令需要配置环境变量才能执行,配置环境变量如下: 系统变量下新建变量名:AXIS2_HOME值:E:\axis2-1.7.4-bin\axis2-1.7.4; 将AXIS2_HOME配置在Path中:%AXIS2_HOME%\bin; 这样AXIS2的环境变量就配置好了,运行自定义的generateCode.bat文件即可生成代码。 Axis2客户端调用代码: package cn.itcast.client; import java.rmi.RemoteException; import axis2.service.server.TestServiceStub; public class Client {
public static void main(String[] args) throws Exception { System.out.println(new Client().aa());
System.out.println(new Client().bb()); } public String aa() throws Exception {
//stub类
TestServiceStub testServiceStub = new TestServiceStub();
//Add方法名:作为一个类new出来
TestServiceStub.Add add = new TestServiceStub.Add();
//将new出来的方法名类作为参数放在要调用的方法里
testServiceStub.add(add);//此处的代码可省略,因为下面已经连写了testServiceStub.add(add).get_return(); return testServiceStub.add(add).get_return();
} public String bb() throws RemoteException{ TestServiceStub testServiceStub = new TestServiceStub();
TestServiceStub.Aaa aaa = new TestServiceStub.Aaa();
aaa.setA("你好周杰伦"); return testServiceStub.aaa(aaa).get_return();
}
}
上一篇:gradle springboot 项目运行的三种方式


下一篇:centos7无故重启-内核升级