JAX-WS 實現WebService發佈

編寫一個普通的類, 再加入一些註解即可.如:

package com.wujianjun.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

import com.wujianjun.domain.Department;
import com.wujianjun.domain.Person;

@WebService(serviceName = "FirstService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class MyServicesImpl {

	@WebMethod()
	public String testSimple(String[] param) {
		String res = "";
		for(String s:param) 
			res+=","+s;
		return res;
	}

	@WebMethod()
	public String testSimple2(Department[] depts) {
		String res = "";
		for(Department s:depts) 
			res+=","+s;
		return res;
	}

	@WebMethod()
	public String testSimple3(Person[] persons) {
		String res = "";
		for(Person s:persons) 
			res+=","+s;
		return res;
	}

}

 直接寫一個普通帶Main的類運行以下語句就可以了

public static void main(String[] args) {
    Endpoint.publish("http://127.0.0.1/services",new MyServicesImpl());
}

再打開瀏覽器輸入 發佈的地址(http://127.0.0.1/services?wsdl)就可以看到對應的wsdl了。

如果用spring來管理對象。則只需要在發佈的時候綁定的對象從spring容器裏取得就可以了。代碼如下:

public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Endpoint.publish("http://192.168.10.179/services", context.getBean("myServices"));
	}

 

這個服務的發佈代碼也可以放到一個servlet的init裏去執行.再把servlet調爲隨服務器啓動即調用.這樣就把service在服務器啓動時發佈出去。

發佈了14 篇原創文章 · 獲贊 1 · 訪問量 2532
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章