Axis1.4 服務器端和客戶端搭建

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

一、創建Maven-web項目

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

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

參考:

https://blog.csdn.net/jack_david/article/details/86532983#tomcat_228

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