WebService
来源:互联网 发布:淘宝的充值平台在哪里 编辑:程序博客网 时间:2024/06/16 11:02
上一篇Axis2使用services.xml进行开发server与client演示了axis2使用service.xml开发webservice服务端与客户端,并未与Spring进行整合。
本篇演示与spring整合下服务端的开发并发布两个service(客户端如何调用,参考上篇)。其实也就是把bean交给Spring容器来管理。
测试上除了你基于SOAP使用Client调用service,Axis2默认情况下还支持Restful风格。当然后者仅支持简单类型参数,二进制文件等不支持。
参考官方文档:axis2与spring整合
【1】环境配置
服务端继续沿用上一个项目,不过要添加spring包/axis2与spring整合jar并修改配置。
① 添加jar;
② 添加applicationContext.xml并配置如下:
只配了自动扫描,说明类使用了注解
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.web"></context:component-scan></beans>
③ 为接口实现类添加@Service注解
④ 修改web.xml 如下:
<?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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <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>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>
⑤ 修改services.xml
路径如下:
配置如下:
<service name="AxisSpringService"> <description>AxisSpringService</description> <!-- SpringBeanName作用类似于普通配置中的ServiceClass,都是用来创建服务类对象,只不过普通配置使用反射来创建 。加入Spring之后,对象的创建交给了Spring的IOC容器,SpringBeanName指定要发布成WebService的Java类,SpringBeanName参数是JavaBean的名称。SpringBeanName固定的不能改 ,因为springWebService是spring中注册的实现类得id。如果不使用spring,可以使用ServiceClass属性,ServiceClass参数要指定要发布成WebService的Java类,并指定全类名的方式。--> <parameter name="SpringBeanName"> myServiceImpl </parameter> <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 --> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <!-- 在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。 例如,getAge方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类, 而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。 --> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
【2】部署到Tomcat,运行
获取的wsdl地址:
http://localhost:8080/Axis2/services/AxisSpringService?wsdl
即,schema+IP+port+contextPath+services+serviceName+?wsdl
targetNamespace如下:
http://impl.service.Axis2.web.com
如果要为option设置action,则示例如下:
Options options = new Options(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setAction("http://impl.service.Axis2.web.com/sayHello");
即,targetNamespace+method
浏览器出现如下图说明正常:
使用上一篇中的客户端进行测试(url变了)
客户端输出结果如下:
使用Restful风格进行测试:
浏览器输入地址:
http://localhost:8080/Axis2/services/AxisSpringService2/sayHello?name=tom
结果如下图:
服务端输出结果如下:
【3】配置两个service
上面演示的一个service下axis2与spring整合。如果两个service呢?下面进行演示。
① 拷贝MyServiceImpl并重命名为MyServiceImpl2
一个接口,两个实现类,发布成两个不同的service。
修改其方法如下:
package com.web.Axis2.service.impl;import org.springframework.stereotype.Service;import com.web.Axis2.service.MyService;@Servicepublic class MyServiceImpl2 implements MyService{ @Override public String sayHello(String name) { //service2 用于区分是第二个service的方法被调用 System.out.println("this is service2 "+name); return "hello "+name; }}
② 修改services.xml
<serviceGroup> <service name="AxisSpringService"> <description>AxisSpringService</description> <parameter name="SpringBeanName"> myServiceImpl </parameter> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service> <service name="AxisSpringService2"> <description>AxisSpringService2</description> <parameter name="SpringBeanName"> myServiceImpl2 </parameter> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service> </serviceGroup>
如上面配置所示,使用了serviceGroup元素。一个xml中只能有一个serviceGroup元素。每个serviceGroup元素下可以有多个service元素,每一个service元素表示一个WebService。
③ 使用不同的url进行测试
两个service对应的url分别如下:
http://localhost:8080/Axis2/services/AxisSpringService?wsdlhttp://localhost:8080/Axis2/services/AxisSpringService2?wsdl
其实就是service名字改变了。
进行测试,服务端输出结果如下:
表示两个service被正常调用!!!
- WebService
- WebService
- webservice
- webservice
- WebService
- webservice
- webService
- WebService
- WebService
- webService
- WEBService
- webService
- WebService
- [WebService]
- webservice
- WebService
- webservice
- WebService
- 【BZOJ1968】约数研究(数论)
- 慕课网——PHP进阶篇(类与面向对象1—5)
- node.js 加载mysql模块问题
- Tomcat启动注册为Windows服务启动
- CentOS 6.5 rpm安装 JDK7
- WebService
- RSA算法原理(一)
- 习题5,5.3
- swift3.0 中class和static
- (四)结构体、共用体、枚举、字节对齐
- 轻松学习RSA加密算法原理综合三篇文章
- turtlebot 利用cartographer 建图
- Android设置默认的数据卡
- ubuntu 安装 php-zip 扩展