axis2与spring集成

本文详细介绍如何将Axis2 Web服务与Spring框架进行集成。包括配置Maven依赖、定义接口及其实现类、配置Spring及Axis2、部署及客户端调用步骤。为读者提供了一个从零开始的实战案例。

首先加入axis2的pom.xml

<!--axis2版本指定-->
<axis2.version>1.6.2</axis2.version>

<!--axis2 begin-->
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2</artifactId>
    <version>${axis2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-spring</artifactId>
    <version>${axis2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-http</artifactId>
    <version>${axis2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-transport-local</artifactId>
    <version>${axis2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.axis2</groupId>
    <artifactId>axis2-xmlbeans</artifactId>
    <version>${axis2.version}</version>
</dependency>
<!--axis2 end-->

创建要发布的接口和实现类

接口:

public interface HelloWorldService{
 
    public String sayHello(String msg);

}

实现类:

@Component("helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService{
    @Override
    public String insertRedPaperWapUser(String msg) {
        return "Hello"+msg;
    }
}

说明:由于要交个spring来管理,此处用@Component组件来注解,

spring配置省略,其他配置,这里只贴出axis2的spring配置:

<!--axis2交给spring来管理-->
<bean id="applicationContext"
      class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />

配置web.xml:

<servlet>
    <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>

接着在你的WEB-INF创建个文件夹services/SimpleService/META-INF/services.xml如图:

202605_DNwT_1471190.png

<?xml version="1.0" encoding="UTF-8"?>

<!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more 
    contributor license agreements. See the NOTICE file ~ distributed with this 
    work for additional information ~ regarding copyright ownership. The ASF 
    licenses this file ~ to you under the Apache License, Version 2.0 (the ~ 
    "License"); you may not use this file except in compliance ~ with the License. 
    You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 
    ~ ~ Unless required by applicable law or agreed to in writing, ~ software 
    distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT 
    WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the 
    License for the ~ specific language governing permissions and limitations 
    ~ under the License. -->
<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
<service name="HelloWorldService">
    <description>axis2与spring集成</description>
    <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
    <parameter name="ServiceObjectSupplier" locked="false">
        org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
    </parameter>
    <!--
       SpringBeanName固定的不能改
       helloWorldService是spring中注册的实现类得id
     -->
    <parameter name="SpringBeanName">helloWorldService</parameter>
    <!--
    在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
    例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,
    而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。
     -->
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                         class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                         class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
</service>

接着,部署tomcat并启动,打开浏览器输入:http://localhost:8080/hnmccClientWap/services/HelloWorldService?wsdl;

客户端调用也很简单:

System.setProperty("javax.net.ssl.trustStore", "../httpsService/target/jetty-ssl.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "axis2key");
String epr = "http://localhost:8080/hnmccClientWap/services/HelloWorldService";

Options options = new Options();
options.setTo(new EndpointReference(epr));
ServiceClient sender = null;
sender = new ServiceClient();
sender.setOptions(options);
OMElement ret = sender.sendReceive(creatMsg());
System.out.println(new String(XXTEA.decryptWithBase64(ret.getFirstElement().getText(), "hnmccztkhdXW45o97YYLwApyehdpVrmy".getBytes(), "utf-8")));


public static OMElement creatMsg() {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://impl.service.xwtec.com",
            "ns1");
    OMElement method = fac.createOMElement("sayHello", omNs);
    OMElement value = fac.createOMElement("msg", omNs);
    value.setText("World!");
    method.addChild(value);
    return method;
}


转载于:https://my.oschina.net/kkrgwbj/blog/639543

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值