Axis1.4 服务器端和客户端搭建

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

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

一、创建Maven-web项目

eclipse

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

idea

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

二、pom.xml  引用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>axis</artifactId>
    <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>

  <name>axis</name>
  <!-- FIXME change it to the project's website -->
  <url>http://maven.apache.org</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>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0</version>
		</dependency>

		<dependency>
			<groupId>axis</groupId>
			<artifactId>axis</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>axis</groupId>
			<artifactId>axis-wsdl4j</artifactId>
			<version>1.5.1</version>
		</dependency>
		<dependency>
			<groupId>javax.mail</groupId>
			<artifactId>mail</artifactId>
			<version>1.4.7</version>
		</dependency>
		
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>
		
    
  </dependencies>

  <build>
  	<finalName>axis</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_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-jar-plugin</artifactId>
          <version>3.0.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>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

三、编写简单的业务逻辑

实现类

package com.spdb.axis.impl;

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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spdb.axis.service.StudentService;
import com.spdb.axis.util.StudAppUtil;


/**
 * 
 * 公共学生信息服务的实现类
 * 
 * @author CJW
 *
 */
public class StudentServiceImpl implements StudentService {


	@Override
	public String getStudentMarks(String strInfoDo) {
		JSONObject map = JSON.parseObject(strInfoDo);
		return StudAppUtil.invokeInnerService(map, "getStudentMarks");
	}

	@Override
	public String getStudentList(String strInfoDo) {
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(System.currentTimeMillis());
	    String time=format.format(date);
	    System.out.println(time);
		JSONArray list = JSON.parseArray(strInfoDo);
		System.out.println(list.get(0));
		return String.valueOf(list.size());
	}

}

接口类

package com.spdb.axis.service;

/**
 * 
 * 公共的学生信息服务接口
 * 
 * @author CJW
 *
 */
public interface StudentService {



	/**
	 * 根据学号和学科号获取成绩
	 * 
	 * @param studentNo
	 *            学生学号
	 * @param subjectNo
	 *            学科号
	 * @return 成绩
	 */
	String getStudentMarks(String strInfoDo);
	
	

	/**
	 * 获取大量LIST
	 * 
	 * @param studentNo
	 *            学生学号
	 * @param subjectNo
	 *            学科号
	 * @return 成绩
	 */
	String getStudentList(String strInfoDo);


}

一个工具类

package com.spdb.axis.util;

import java.util.Map;

/**
 * 
 * 公共学生信息工具类
 * 
 * @author CJW
 *
 */
public class StudAppUtil {

	/**
	 * 
	 * 调用内部服务
	 * 
	 * @param dataMap
	 *            请求数据集
	 * @param txnCode
	 *            交易码
	 * @return 返回交易的结果信息
	 */
	public static String invokeInnerService(Map<String,Object> dataMap, String txnCode) {
		
		String responseInfo = "";
		
		 if("getStudentMarks".equals(txnCode)){
			String studentNo = (String)dataMap.get("studentNo");
			String subjectNo = (String)dataMap.get("subjectNo");
			responseInfo = "学号=" + studentNo +",学科号="+subjectNo + ",成绩=98";
		}
		System.out.println(responseInfo);
		return responseInfo;
	}
}

四、配置WEB.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- WebService接入配置  BEGIN -->
  <servlet>  
        <servlet-name>AxisServlet</servlet-name>  
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>AxisServlet</servlet-name>  
        <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>
    <!-- WebService接入配置  END -->
</web-app>

创建deploy.wsdd 文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- WebService接入配置  BEGIN -->
  <servlet>  
        <servlet-name>AxisServlet</servlet-name>  
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>AxisServlet</servlet-name>  
        <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>
    <!-- WebService接入配置  END -->
</web-app>

五、创建 server-config.wsdd

1.DOS命令窗口下切换到我们应用的部署路径下面,我的本地的部署路径为:D:\MyDeveloper\normal\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\axis\WEB-INF

(tomcat启动打印的内容,能找到部署路径)

2.在该路径下执行如下一行命令:

java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -l http://127.0.0.1:7777/axis/services deploy.wsdd

执行成功后如下图:说明server-config.wsdd文件生成成功了

å¨è¿éæå¥å¾çæè¿°

六、复制server-config.wsdd到项目中去

七、启动TOMCAT

浏览器输入后

http://localhost:7777/axis/services/StudentService?wsdl
显示如下即表示成功

以上就是服务器端的全部代码结构

下面创建一个简单的客户端验证报文发送

一、创建maven项目

二、Pom.xml 添加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>
    
         <dependency>
            <groupId>axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>axis</groupId>
            <artifactId>axis-wsdl4j</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
    
  </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>

建立一个程序Test文件

package com;


import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

public class Test {

	public static void main(String[] args) {
		 try {
			 
			    String xmlStr="123";
	            String result2= invoke("http://localhost:7777/axis/services/StudentService","getStudentInfo",xmlStr);
	            System.out.println(result2);
	        } catch (Exception e) {
	        	e.printStackTrace();
	        }
	}
	
    /*
     * 调用webservice路口
     * @param endpoint 地址
     * @param methodName 调用的方法
     * @param xmlStr 传递的xml字符串参数
     * @return
     */
    public static String invoke(String endpoint, String methodName, String xmlStr) {
        Service service = new Service();
        Call call = null;
        try {
            call = (Call) service.createCall();
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        QName qn = new QName(methodName);
        call.setOperationName(qn);
        call.setTargetEndpointAddress(endpoint);
        call.setUseSOAPAction(true);
        String result = "";
        try {
            // 给方法传递参数,并且调用方法
            result = (String) call.invoke(new Object[]{xmlStr});
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return result;
    }
}

启动运行,有值返回则说明服务器端和客户端。通信成功

源码:

服务器端:https://github.com/chenjunwei111/axis1.4

客户端:https://github.com/chenjunwei111/axisCus

参考:

使用axis1.4 发布WebService 服务_Jack_David的专栏-CSDN博客_axis1.4 webservice

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值