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被正常调用!!!

目录
相关文章
|
1月前
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
3月前
|
XML Java 数据格式
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
37 1
|
6天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
57 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
1月前
|
XML Java 数据格式
Spring5入门到实战------3、IOC容器-Bean管理XML方式(一)
这篇文章详细介绍了Spring框架中IOC容器的Bean管理,特别是基于XML配置方式的实现。文章涵盖了Bean的定义、属性注入、使用set方法和构造函数注入,以及如何注入不同类型的属性,包括null值、特殊字符和外部bean。此外,还探讨了内部bean的概念及其与外部bean的比较,并提供了相应的示例代码和测试结果。
Spring5入门到实战------3、IOC容器-Bean管理XML方式(一)
|
1月前
|
XML Java 数据格式
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
这篇文章是Spring5框架的实战教程,主题是IOC容器中Bean的集合属性注入,通过XML配置方式。文章详细讲解了如何在Spring中注入数组、List、Map和Set类型的集合属性,并提供了相应的XML配置示例和Java类定义。此外,还介绍了如何在集合中注入对象类型值,以及如何使用Spring的util命名空间来实现集合的复用。最后,通过测试代码和结果展示了注入效果。
Spring5入门到实战------4、IOC容器-Bean管理XML方式、集合的注入(二)
|
1月前
|
XML Java 数据格式
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
这篇文章是Spring5框架的AOP切面编程教程,通过XML配置方式,详细讲解了如何创建被增强类和增强类,如何在Spring配置文件中定义切入点和切面,以及如何将增强逻辑应用到具体方法上。文章通过具体的代码示例和测试结果,展示了使用XML配置实现AOP的过程,并强调了虽然注解开发更为便捷,但掌握XML配置也是非常重要的。
Spring5入门到实战------11、使用XML方式实现AOP切面编程。具体代码+讲解
|
1月前
|
XML Java 数据格式
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
这篇文章是Spring5框架的入门教程,详细讲解了IOC容器中Bean的自动装配机制,包括手动装配、`byName`和`byType`两种自动装配方式,并通过XML配置文件和Java代码示例展示了如何在Spring中实现自动装配。
Spring5入门到实战------6、IOC容器-Bean管理XML方式(自动装配)
|
1月前
|
XML Java 数据库
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
这篇文章是Spring5框架的实战教程,详细介绍了事务的概念、ACID特性、事务操作的场景,并通过实际的银行转账示例,演示了Spring框架中声明式事务管理的实现,包括使用注解和XML配置两种方式,以及如何配置事务参数来控制事务的行为。
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
|
1月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
32 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
26天前
|
Java Spring
【Azure Service Bus】使用Spring Cloud integration示例代码,为多个 Service Bus的连接使用 ConnectionString 方式
【Azure Service Bus】使用Spring Cloud integration示例代码,为多个 Service Bus的连接使用 ConnectionString 方式