weblogic部署axis2實現的webservice服務的web工程(非打war包)

首先聲明哈借鑑了別人的貢獻,再稍微詳細點做個記錄。
下面步驟的前提是你的weblogic是得是部署好的哈,可以登錄console直接部署的。
1.創建一個web工程(非maven,因爲maven的web項目目錄級別比較亂,沒調試通)
在這裏插入圖片描述在這裏插入圖片描述在這裏插入圖片描述這樣我們就有一個web工程了

2.在這裏插入圖片描述到官網上下載axis2的包
官網:http://axis.apache.org/axis2/java/core/
下載後進行解壓得到war包
在這裏插入圖片描述解壓開war包
在這裏插入圖片描述
a.將axis2-web粘到工程WebContent下
b.將解壓開的war包裏WEB-INF/裏面的lib的包,粘到工程中的WebContent/WEB-INF/lib下
c.將解壓開的war包裏WEB-INF/裏面modules,conf文件夾粘到工程中的WebContent/WEB-INF下
在這裏插入圖片描述
d.在如上圖的目錄下創建services目錄以及如下一直到services.xml文件

services.xml文件內容爲:

<?xml version="1.0" encoding="UTF-8"?>
<service name="Testservice">  <!-- 指定服務名,隨便定義 -->
    <description>測試axis2webservices</description><!-- 服務的作用說明,可寫可不寫 -->
     <!-- 指定要發佈的類路徑 -->
    <parameter name="ServiceClass">  <!-- 自定義    name-->
           axis2.WebserviceDemo     <!-- 寫的類路徑 -->
    </parameter>  
   <!-- 類裏面的方法,有其他方法就在寫個operation標籤 -->
    <operation name="sayHello">  <!-- 類裏面的方法名 -->
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
    </operation>   
</service>

e.web.xml文件內容
注意下面的param-value,按照weblogic在服務器的對應位置進行修改

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>webservice</display-name><!-- 項目名,創建項目時候自動生成的 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  
    <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
        <!--下面配置是支持Axis2在Weblogic可發佈服務,value指的是域下對應Web項目包WEB-INF下,其實就是找到拷貝過去的那三個文件夾-->
        <init-param>
            <param-name>axis2.repository.path</param-name>
            <param-value>/home/weblogic/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain/servers/AdminServer/tmp/_WL_user/webservice/byjuto/war/WEB-INF</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
  
  
  
</web-app>

f.weblogic.xml內容

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://www.bea.com/ns/weblogic/weblogic-web-app" 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 http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
    <wls:weblogic-version>10.3.0</wls:weblogic-version>
    <wls:context-root>E7-Planning</wls:context-root>
    <wls:container-descriptor>
        <wls:servlet-reload-check-secs>-1</wls:servlet-reload-check-secs>
        <wls:resource-reload-check-secs>-1</wls:resource-reload-check-secs>
        <wls:prefer-web-inf-classes>true</wls:prefer-web-inf-classes>
    </wls:container-descriptor>
    <wls:session-descriptor>
        <wls:cookie-name>JSESSIONID1</wls:cookie-name>
        <wls:encode-session-id-in-query-params>true</wls:encode-session-id-in-query-params>
    </wls:session-descriptor>
</wls:weblogic-web-app>

所有的配置文件就這些了,具體的結構如下

在這裏插入圖片描述
然後開始填代碼

package axis2;


public class WebserviceDemo {
	 public String  sayHello(String name){  
	        return "hello " + name;  
	    }  

	

}

這個是提供webservice服務的實現類,注意接口裏面的方法要是public的,然後把services.xml文件中對應的內容修改一下。然後就可以將工程打成war包進行部署了,具體打包以及weblogic部署步驟就不詳細記錄了,可自行百度。

部署完成之後,在頁面上進行訪問在這裏插入圖片描述看到上面的接口信息就證明接口是打開的。
具體的url格式如下:
ip:端口/weblogic.xml文件中的context-root值/services/services.xml文件的service name值?wsdl

接口確認打開之後就可以自行編寫客戶端進行測試。

package axis2;



import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class TextClient {
	 public static void main(String[] args) {
		    
	        try {
	            //本機tomcat端口默認爲8081,參數是wsdl網址的一部分
	      
	            EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:7001/E7-Planning/services/Testservice");
	        	RPCServiceClient sender = new RPCServiceClient();  
	            Options options = sender.getOptions();  
	            options.setTimeOutInMilliSeconds(2*20000L);//超時時間20s  
	            options.setTo(targetEPR);  
	            /**
	             * 參數:
	             * 1:在網頁上執行 wsdl後xs:schema標籤的targetNamespace路徑
	             * <xs:schema  targetNamespace="http://test.axiswevservice.com">
	             * 2:<xs:element name="test"> ======這個標籤中name的值
	             * 
	             */
	            QName qname = new QName("http://axis2", "sayHello"); 
	            String str = "客戶端調用成功";  //方法的入參
	            Object[] param = new Object[]{str};  
	            Class<?>[] types = new Class[]{String.class};  //這是針對返值類型的  
	            /** 
	             * RPCServiceClient類的invokeBlocking方法調用了WebService中的方法。 
	             * invokeBlocking方法有三個參數 
	             * 第一個參數的類型是QName對象,表示要調用的方法名; 
	             * 第二個參數表示要調用的WebService方法的參數值,參數類型爲Object[]; 
	             * 第三個參數表示WebService方法的返回值類型的Class對象,參數類型爲Class[]。 
	             *  
	             * 當方法沒有參數時,invokeBlocking方法的第二個參數值不能是null,而要使用new Object[]{}。 
	             */  
	            Object[] response1 = sender.invokeBlocking(qname, param, types);  
	            System.out.println(response1[0]);
	        } catch (AxisFault e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        
	    }

}

在這裏插入圖片描述成功啦!

在網上綜合了好幾版內容纔在weblogic上調通,送給大家不用謝( ̄▽ ̄)~*

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章