WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)

简介: WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)

本篇演示与spring整合下服务端的开发并发布两个service(客户端如何调用,参考上篇)。其实也就是把bean交给Spring容器来管理。


测试上除了你基于SOAP使用Client调用service,Axis2默认情况下还支持Restful风格。当然后者仅支持简单类型参数,二进制文件等不支持。


【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"); 

浏览器出现如下图说明正常:


使用上一篇中的客户端进行测试(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;
@Service
public 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进行测试

http://localhost:8080/Axis2/services/AxisSpringService?wsdl
http://localhost:8080/Axis2/services/AxisSpringService2?wsdl

其实就是service名字改变了。

查看第一个wsdl文件:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://impl.service.Axis2.web.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" targetNamespace="http://impl.service.Axis2.web.com">
<wsdl:documentation>AxisSpringService</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://impl.service.Axis2.web.com">
<xs:element name="sayHello">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="sayHelloResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="parameters" element="ns:sayHello"/>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part name="parameters" element="ns:sayHelloResponse"/>
</wsdl:message>
<wsdl:portType name="AxisSpringServicePortType">
<wsdl:operation name="sayHello">
<wsdl:input message="ns:sayHelloRequest" wsaw:Action="urn:sayHello"/>
<wsdl:output message="ns:sayHelloResponse" wsaw:Action="urn:sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AxisSpringServiceSoap11Binding" type="ns:AxisSpringServicePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="urn:sayHello" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="AxisSpringServiceSoap12Binding" type="ns:AxisSpringServicePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="sayHello">
<soap12:operation soapAction="urn:sayHello" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="AxisSpringServiceHttpBinding" type="ns:AxisSpringServicePortType">
<http:binding verb="POST"/>
<wsdl:operation name="sayHello">
<http:operation location="sayHello"/>
<wsdl:input>
<mime:content type="application/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="application/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AxisSpringService">
<wsdl:port name="AxisSpringServiceHttpSoap11Endpoint" binding="ns:AxisSpringServiceSoap11Binding">
<soap:address location="http://localhost:8080/Axis2/services/AxisSpringService.AxisSpringServiceHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="AxisSpringServiceHttpSoap12Endpoint" binding="ns:AxisSpringServiceSoap12Binding">
<soap12:address location="http://localhost:8080/Axis2/services/AxisSpringService.AxisSpringServiceHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="AxisSpringServiceHttpEndpoint" binding="ns:AxisSpringServiceHttpBinding">
<http:address location="http://localhost:8080/Axis2/services/AxisSpringService.AxisSpringServiceHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这里需要额外说明,Axis2默认同时支持SOAP1.1、SOAP1.2和HTTP协议。即厂家的SOAP和REST调用方式。如下图所示:


进行测试,服务端输出结果如下:


表示两个service被正常调用!!!

目录
相关文章
|
2月前
|
XML Java 数据格式
Spring IoC容器初始化过程(xml形式)
Spring IoC容器初始化过程(xml形式)
46 0
|
4月前
|
XML Java uml
spring 源码解析——第一篇(ioc xml方式)
spring 源码解析——第一篇(ioc xml方式)
32 0
|
4月前
|
XML Java 数据格式
②【Spring】一文精通:IOC - 基于XML方式管理Bean
②【Spring】一文精通:IOC - 基于XML方式管理Bean
146 0
|
1月前
|
网络安全
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
ssh(Spring+Spring mvc+hibernate)——applicationContext.xml
7 0
|
2月前
|
Java 数据库 开发者
Spring注解大揭秘:@Component、@Service、@Repository详解
Spring注解大揭秘:@Component、@Service、@Repository详解
35 0
|
2月前
|
XML 运维 Java
spring事务(3)基于XML的声明式事务
spring事务(3)基于XML的声明式事务
17 0
|
3月前
|
XML Java 数据库连接
Spring Boot整合Mybatis(注解版+XML版)
Spring Boot整合Mybatis(注解版+XML版)
54 0
|
3月前
|
XML Java 数据格式
Spring5源码(17)-Spring解析xml默认命名空间
Spring5源码(17)-Spring解析xml默认命名空间
22 0
|
3月前
|
XML JavaScript Java
Spring5源码(16)-Spring将Xml文件解析为Document对象
Spring5源码(16)-Spring将Xml文件解析为Document对象
38 0
|
3月前
|
XML Java 数据格式
Spring AOP(注解方式和XML方式)
Spring AOP(注解方式和XML方式)
126 0