spring與axis2整合發布webservice


     最近在研究整合spring框架和axis2發布webservice服務,由於本人也才學java不久,為了便於以后的查看,在這里記錄下發布過程。

  所需的工具包,spring.jar和axis2鏈接地址為http://pan.baidu.com/s/1gdgVBoB,這里發布服務只需要兩個包,spring-framework-3.2.1.RELEASE-dist.zip和axis2-1.6.2-war.zip。首先解壓axis2-1.6.2-war.zip,得到axis2.war文件,放入tomcat的webapps目錄中,啟動tomcat,瀏覽器中輸入http://ip:端口號/axis2/出現

說明axis2運行成功,會在webapps中生成一個叫axis2的目錄。接着在myeclipse中新建一個web工程,這里我取名為WjWebservice,工程目錄結構

解壓上面的兩個包,將其中的jar包全部放入WebRoot/WEB-INF/lib目錄中,在WEB-INF目錄下新建conf和modules目錄,將tomcat的webapps/axis2/WEB-INF下的conf和modules目錄中的文件分別倒入在web工程中新建的conf和modules下。新建services目錄,新建test(這個目錄名字可以自己隨便取)目錄,新建META-INF目錄,新建services.xml。

  在com.wj.service中新建HelloWorld接口,代碼為:

package com.wj.service;

public interface HelloWorld {
    public String greeting(String name);
    public void print();
}

  在com.wj.service中新建HelloWorldImpl類實現HelloWorld接口,代碼為:

package com.wj.service;

public class HelloWorldImpl implements HelloWorld{

    public String greeting(String name) {
        
        return name+"hello world!";
    }

    public void print() {
        System.out.println("Hi!");
        
    }

}

  在com.wj.service中新建webservice javabean對象HelloWorldService類,代碼為:

package com.wj.service;

public class HelloWorldService {
    public HelloWorld helloWorld;

    public HelloWorld getHelloWorld() {
        return helloWorld;
    }

    public void setHelloWorld(HelloWorld helloWorld) {
        this.helloWorld = helloWorld;
    }
    public String sayGreeting(String name)
    {
        return helloWorld.greeting(name);
    }
    public void sayPrint()
    {
        helloWorld.print();
    }
}    

  配置spring的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:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  

    <bean id= "applicationContext" class = "org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"  />
    <bean id="helloWorldImpl" class="com.wj.service.HelloWorldImpl">  
    </bean>  
    <bean id="helloWorldService" class="com.wj.service.HelloWorldService">
        <property name="helloWorld" ref="helloWorldImpl"></property>
    </bean>
</beans>

  配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    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">
    <!-- 注冊axis2的servlet -->
    <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>  
     <!-- 加載spring的配置文件  -->
    <context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>classpath:applicationContext.xml</param-value>  
    </context-param>  
    <!-- 增加spring監聽器 -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

  配置services.xml:

<?xml version="1.0" encoding="UTF-8"?>
<service name= "helloWorldWebservice" targetNamespace="http://www.wujianjun.org">
        <description>simple spring example</description>
        <parameter name= "ServiceObjectSupplier" >
            org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
        </parameter>
        <parameter name= "SpringBeanName" >helloWorldService</parameter>
        <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>

  瀏覽器中輸入:http://IP:端口/工程名/services/services.xml配置的服務名稱?wsdl生成wsdl文件

到此webservice服務發布完成。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM