使用Axis簡單步驟發佈Webservice

1.下載axis 1.4

2.創建MyEclipse項目,將axis 1.4 lib目錄下的jar包放到工程的WEB-INF/lib目錄下

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<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>*.jws</url-pattern>
		<url-pattern>/services/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

4.開發Service

package axis.example.service;

import java.util.HashMap;

import axis.example.pojo.UserInfo;

public class GetUserInfoService {
	private HashMap<String, UserInfo> users = new HashMap<String, UserInfo>();
	
	public GetUserInfoService() {
		UserInfo u = new UserInfo();
		u.setId(1);
		u.setName("AAA");
		u.setEmail("[email protected]");
		u.setAge(22);
		users.put(u.getName(), u);
		u.setName("BBB");
		u.setEmail("[email protected]");
		u.setAge(22);
		users.put(u.getName(), u);
	}
	
	public UserInfo getUserInfo(String name) {
		return users.get(name);
	}
}
package axis.example.pojo;

import java.io.Serializable;

public class UserInfo implements Serializable {
	private int id;
	private String name;
	private int age;
	private String email;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

5.發佈Service

在src目錄下創建server-config.wsdd,內容如下

<?xml version="1.0" encoding="UTF-8"?>
<!-- axis webservice config file -->
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
  <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>
  
  <globalConfiguration>
  	  <!-- 默認爲true,改爲false後,
  	  	客戶端如果使用普通http調用方式得到的xml數據中將不會出現multiRef標籤 -->
	  <parameter name="sendMultiRefs" value="false"/>
	  <!-- 默認爲true,改變返回xml數據中不含類型信息 -->
	  <parameter name="sendXsiTypes" value="false"/>
	  <!-- 
	  <parameter name="enable2DArrayEncoding" value="true"/>
	   -->
  </globalConfiguration>
  
  <service name="GetUserInfoService" provider="java:RPC">
  	<parameter name="className" value="axis.example.service.GetUserInfoService"/>
  	<parameter name="allowedMethods" value="getUserInfo"/>
  </service>
  
  <transport name="http">
  	<requestFlow>
  		<handler type="URLMapper"/>
  	</requestFlow>
  </transport>
</deployment>
6.運行

當使用http://localhost:8088/AxisExample/services/GetUserInfoService?wsdl
訪問時得到如下內容,說明發布webservcie成功


7.說明

第一次訪問wsdl的時候出現

Unable to find config file.  Creating new servlet engine config file: /WEB-INF/server-config.wsdd

不知道是不是因爲我沒有deploy的時候做一些其他配置,比如我在網上看見還要創建deploy.wsdd,還要tomcat中配置axis。這些我都沒有配置,卻能夠正常調用。不知道是不是版本差異所造成的。



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