WebService-入門程序

jdk :1.7 eclipse  相應的webservice jar包

目錄 如圖

新建ws 服務端 :

類依次爲:接口,實現類,發佈類

接口代碼:

package com.xhs.ws.jaxws;

public interface WeatherInterface {
	/**
	 * 天氣查詢
	 */
	public String queryWeather(String cityName);
}
實現類代碼:

package com.xhs.ws.jaxws;

import javax.jws.WebService;

@WebService
public class WeatherInterfaceImpl implements WeatherInterface {

	@Override
	public String queryWeather(String cityName) {
		System.out.println("from client .." + cityName);
		String weather = "大熱天";
		return weather;
	}

}

發佈類代碼:

package com.xhs.ws.jaxws;

import javax.xml.ws.Endpoint;

public class WeatherServer {
	public static void main(String[] args) {
		/**
		 * address  服務地址
		 * implementor 實現類
		 */
		Endpoint.publish("http://127.0.0.1:12345/weather", new WeatherInterfaceImpl());
	}
}

客戶端:

第一個包爲生成的代碼

其生成的命令爲 :先進入本地src目錄 :

D:\Eclipse_Space\space\ws_client\src>wsimport -s .http://127.0.0.1:12345/weather?wsdl

第二個包爲客戶端代碼 :

package com.xhs.ws.weatherclient;

import com.xhs.ws.jaxws.WeatherInterfaceImpl;
import com.xhs.ws.jaxws.WeatherInterfaceImplService;

public class weatherClient {
	public static void main(String[] args) {
		// 第一步:創建服務視圖,視圖是從service標籤name熟悉獲取
		WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
		// 第二步:獲取服務實現類,實現類是從portType標籤name屬性獲取
		WeatherInterfaceImpl WeatherInterfaceImpl = weatherInterfaceImplService.getPort(WeatherInterfaceImpl.class);
		// 第三步:獲取查詢方法,從portType的operation屬性獲取
		String weather = WeatherInterfaceImpl.queryWeather("長沙");
		System.out.println(weather);
	}
}
運行 :
from client ..長沙

大熱天



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