今天學習webservice,主要參考了網絡上的一些文章。html
1.關於原理的介紹:我的認爲這篇文章寫得不錯了,戳這裏。java
2.關於demo的編寫:我的認爲這篇文章很簡潔,也能運行成功,戳這裏。web
按照上面那篇文章的demo運行成功之後,我想在java web中加入一個webservice服務,供其餘的java web項目調用。spring
1.首先,在項目中添加com.xdx.webservice包,如圖所示。瀏覽器
2.在其中加入WetherServiceInter接口和其實現類WeatherService類。代碼以下。websocket
package com.xdx.webservice; import javax.jws.WebMethod; import javax.jws.WebService; import com.xdx.entity.TAdmin; @WebService public interface WeatherServiceInter { @WebMethod public String getWeatherByCityname(String name); public TAdmin getAdminById(int adminId); }
package com.xdx.webservice; import javax.jws.WebService; import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; import com.xdx.entity.TAdmin; import com.xdx.service.AdminService; @WebService public class WeatherService implements WeatherServiceInter { public String getWeatherByCityname(String name) { System.out.println(name+"天氣晴朗"); return name+"天氣晴朗"; } public TAdmin getAdminById(int adminId){ WebApplicationContext webApplicationContext = ContextLoader .getCurrentWebApplicationContext(); AdminService adminService=(AdminService) webApplicationContext.getBean("adminService"); TAdmin admin = adminService.getAdminById(adminId); return admin; } }
3.發佈webservice服務,這邊咱們在web項目啓動的時候發佈webservice服務,創建一個filter,在filter中寫入以下代碼,負責啓動websocket(以前的demo)和webservice。這邊咱們選用8085端口做爲webservice的端口。網絡
package com.xdx.filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.xml.ws.Endpoint; import org.java_websocket.WebSocketImpl; import com.xdx.webservice.WeatherService; import com.xdx.websocket.WsServer; public class StartFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { } public void init(FilterConfig arg0) throws ServletException { this.startWebsocketInstantMsg(); } /** * 啓動即時聊天服務 */
public void startWebsocketInstantMsg() { WebSocketImpl.DEBUG = false; WsServer s; s = new WsServer(8887); s.start(); System.err.println("websocket啓動了"); //啓動webservice
Endpoint.publish("http://localhost:8085/ws_server/weather", new WeatherService()); System.out.println("webservice啓動了"); } }
4.在web.xml中配置filter,配置以下。app
<!-- filter -->
<filter>
<filter-name>startFilter</filter-name>
<filter-class>com.xdx.filter.StartFilter</filter-class>
</filter>
5.運行項目,在瀏覽器輸入http://localhost:8085/ws_server/weather?wsdl 測試一下,會出現以下的xml文件。socket
說明已經發布成功了。學習
6.接下來咱們在另一個web項目(名爲m_gecko)中來調用該webservice服務裏面的方法。首先,經過cmd進入m_gecko項目的src/main/java目錄下,如圖所示,輸入wsimport -keep http://localhost:8085/ws_server/weather?wsdl
出現上述提示,說明已經在m_gecko項目中生成了webservice的客戶端代碼,咱們刷新一下m_gecko項目看看。果真是的。
7.在m_gecko項目中咱們來寫調用webservice服務的方法。首先咱們來調用getWeatherByCityname方法。
@ResponseBody
@RequestMapping(value="webserviceDemo1", produces = "text/html; charset=utf-8")
public String webserviceDemo1(){
WeatherServiceService factory=new WeatherServiceService();
WeatherService service=factory.getWeatherServicePort();
String result=service.getWeatherByCityname("廈門");
return result;
}
運行m_gecko項目(同時也要運行webservice服務所在的項目warrior項目),在瀏覽器中輸入http://localhost:8080/m_gecko/webserviceDemo1。
而後再來調用getAdminById方法。
@ResponseBody @RequestMapping(value="webserviceDemo2") public TAdmin webserviceDemo2(){ WeatherServiceService factory=new WeatherServiceService(); WeatherService service=factory.getWeatherServicePort(); TAdmin admin=service.getAdminById(5); return admin; }
效果以下