一 开胃菜

    工作需要发布接口,之前用axis2+ssm框架是项目经理搭建的,我再进行开发,今天闲下来,自己学学,千辛万苦整出来了。

二 axis2准备,去人家官网把需要的文件下载了

    Axis2整合Spring,自己搭建框架

这是我从官网摘下来的文件,然后按照网上资料进行安装,我就不自己写了,从人家那贴过来。一下就是:

Axis2的下载和安装

    Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物。Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持Spring、JSON等技术。这些都将在后面的系列教程中讲解。在本文中主要介绍了如何使用Axis2开发一个不需要任何配置文件的WebService,并在客户端使用Java和C#调用这个WebService。 

一、Axis2的下载和安装 
    读者可以从如下的网址下载Axis2的最新版本: 
    http://ws.apache.org/axis2/ 
    在本文使用了目前Axis2的最新版本1.4.1。读者可以下载如下两个zip包: 
    axis2-1.4.1-bin.zip 
    axis2-1.4.1-war.zip 
    其中axis2-1.4.1-bin.zip文件中包含了Axis2中所有的jar文件, axis2-1.4.1-war.zip文件用于将WebService发布到Web容器中。 
    将axis2-1.4.1-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到<Tomcat安装目录>\webapps目录中(本文使用的Tomcat的版本是5.5),并启动Tomcat。 
    在浏览器地址栏中输入如下的URL:     http://localhost:8080/axis2/ 

    如果在浏览器中显示出如图1所示的页面,则表示Axis2安装成功。 

Axis2整合Spring,自己搭建框架

小伙伴按照以上就把axis2安装成功了。再往下唠。

三创建动态网站项目

3.1

    lib导入jar包=spring+axis2所有jar包。

    web.xml文件加载spring和arix2,如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>AxisTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <!-- _______加载spring__________-->

  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:ApplicationContext.xml</param-value>
    </context-param>
   <!-- _______________________-->
   <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>
 <!-- ___________这是tomcat7版本以上自带cors解决跨域问题的,可以不加____________-->
     <filter>
          <filter-name>CorsFilter</filter-name>  
          <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>  
    </filter>  
    <filter-mapping>  
          <filter-name>CorsFilter</filter-name>  
          <url-pattern>/*</url-pattern>  
    </filter-mapping>  
</web-app>

 3.2

之前把aris2.war不是放在本地tomcat/webapps文件夹下嘛,把将conf 、axis2-web(可加可不加) 、modules文件夹移动到 自己工程的 下各个对应的位置,如图

Axis2整合Spring,自己搭建框架


Axis2整合Spring,自己搭建框架

3.3 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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.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">
     <!-- <context:annotation-config />
    <context:component-scan base-package="com.wazh.ws"></context:component-scan> -->
</beans>

3.4 services.xml

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
    <service name="helloService" targetNamespace="http://inf.axis2/"
        scope="application">
        <description>消防预案WebService</description>
        <schema schemaNamespace="http://inf.axis2/" />
        <parameter name="ServiceClass" locked="false">com.wazh.ws.HelloService
        </parameter>
        <!-- 想用spring来管理接口实现类,可是就是不成功 -->
        <!--  <parameter name="SpringBeanName">springWebService</parameter> -->
        <messageReceivers>
        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver  mep="http://www.w3.org/ns/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
    </messageReceivers>
    </service>
</serviceGroup>

3.5实现类

package com.wazh.ws;

public class HelloService {
    public String sayHelloNew() {
        return "hello";
    }

    public String sayHelloToPersonNew(String name) {
        if (name == null) {
            name = "nobody";
        }
        return "hello," + name;
    }

    public void updateData(String data) {
        System.out.println(data + " 已更新。");
    }
}

希望能帮助自己以后搭建项目,也希望能帮到大家,有问题请反馈或者留言,qq:1228029181



相关文章: