axis2.0 服务器端和客户端搭建

20 篇文章 2 订阅
5 篇文章 0 订阅

学以致用,欢迎转载,更多联系QQ:289325414

一、创建maven项目

eclipse

(请参考)eclipse 创建maven-web项目_CJ点的博客-CSDN博客

idea

idea maven创建web项目 - 张橙子 - 博客园

二、引用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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值