axis2.0 服務器端和客戶端搭建

存於世,必要拯救世界,希望的燈光也需要積累。
記之已身,學以致用,歡迎轉載,更多聯繫QQ:289325414

一、創建maven項目

(請參考)https://blog.csdn.net/qq_37203082/article/details/100557128

二、引用jar包

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.spdb</groupId>
  <artifactId>axis2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>axis2 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
         <version>1.2.28</version>
    </dependency>
    
     <!--servlet依賴-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!--服務端-->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.4.0</version>
        </dependency>
    
  </dependencies>

  <build>
    <finalName>axis2</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

三、搭建基本架子,紅框處的文件夾需要自己手動創建

四、配置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>

五、寫好JAVA,做一個簡單判斷返回

import java.sql.Date;
import java.text.SimpleDateFormat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

public class TestService {

	  public String test(String param) {
	        System.out.println("服務端被請求了一次....");
			return "axis2 response"+param;
	    }
	  
	  
	  
}

六、創建好services.xml文件加上內容

<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
    <!-- 指定服務名,隨便定義 -->
    <service name="ZipKinService">
        <!-- 服務的作用說明,可寫可不寫 -->
        <description>測試axis2webservices</description>
        <!-- 指定要發佈的類路徑  自定義name-->
        <parameter name="ServiceClass">
            com.axis2.TestService
        </parameter>
        <!-- 類裏面的方法名 ,若有多個方法,可以新增operation標籤 -->
        <operation name="test">
            <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
        </operation>
    </service>
</serviceGroup>

7、配置完成後,將項目放到TOMCAT啓動

網頁輸入

http://localhost:7777/axis2/services/ZipKinService?wsdl

看到以下頁面說明成功

搭建客戶端

一、同樣創建好maven項目

二、引用jar包

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.spdb</groupId>
  <artifactId>axisCus</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>axisCus Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
   


      <!--axis2 客戶端-->
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-adb</artifactId>
          <version>1.6.2</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-kernel</artifactId>
          <version>1.6.2</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-transport-local</artifactId>
          <version>1.6.2</version>
      </dependency>

      <dependency>
          <groupId>org.apache.axis2</groupId>
          <artifactId>axis2-transport-http</artifactId>
          <version>1.6.2</version>
      </dependency>
      <!--axis2 客戶端-->


  </dependencies>

  <build>
    <finalName>axisCus</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

三、寫測試方法類

package com;

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

import javax.xml.namespace.QName;

/**
* Description 測試axis2.0
* @Author junwei
* @Date 16:10 2019/9/5
**/
public class TestAxis2 {

    public static void main(String[] args) {
        String xmlStr="報文發送";
        String res = connectAxis2("http://localhost:7777/axis2/services/ZipKinService","test", xmlStr);
        System.out.println(res);
    }



    public static String connectAxis2(String url,String methor, String parm) {
        try {
            //本機tomcat端口爲7777,參數是wsdl網址的一部分
            EndpointReference targetEPR = new EndpointReference(url);
            RPCServiceClient sender = new RPCServiceClient();
            Options options = sender.getOptions();
            //超時時間20s
            options.setTimeOutInMilliSeconds(2 * 20000L);
            options.setTo(targetEPR);
            /**
             * 參數:
             * 1:在網頁上執行 wsdl後xs:schema標籤的targetNamespace路徑
             * <xs:schema  targetNamespace="http://axis2.com">
             * 2:<xs:element name="test"> ======這個標籤中name的值
             */
            QName qname = new QName("http://axis2.com", methor);
            //方法的入參
            String str = parm;
            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[] response = sender.invokeBlocking(qname, param, types);
            return response[0].toString();
        } catch (AxisFault e) {
            e.printStackTrace();
            return "";
        }

    }
}

執行方法有還回,說明客戶端和服務器已打通。

注:axis2.0可向axis1.4發送報文,但反過來不行。 向下兼容,向上不兼容

源碼

服務器端:

https://github.com/chenjunwei111/axis2.0

客戶端:

https://github.com/chenjunwei111/axisCus

參考:

https://blog.csdn.net/s740556472/article/details/79680454#commentBox

 

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