Axis2.0サーバーとクライアントの構築

あなたが学んだことを適用し、再版へようこそ、詳細についてはQQ:289325414にお問い合わせください

1つは、Mavenプロジェクトを作成する

日食

(参照してください)https://blog.csdn.net/qq_37203082/article/details/100557128

考え

https://www.cnblogs.com/zhangchengzi/p/9865546.html

次に、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>

3.基本的な棚を作成するには、赤いボックス内のフォルダーを自分で手動で作成する必要があります

第四に、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;
	    }
	  
	  
	  
}

6つ目は、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に入れて開始します

Web入力

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

成功を示すには、次のページを参照してください

クライアントを構築する

1つは、優れた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>

3、テストメソッドクラスを書く

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

 

おすすめ

転載: blog.csdn.net/qq_37203082/article/details/100560055