已有ssh專案中加入webservice(CXF)
一、服務端實現。
1.CXF必須包;
2.applicationcontext.xml裡面加入
(1)標頭檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
(2)暴露介面,訪問地址為http://localhost:8080/YYY/ewebservice;
<jaxws:endpoint id="s2" implementor="cn.com.webserviceImpl.storageShIm" address="/s2" > </jaxws:endpoint>
3.攔截器為CXF放行
(1)<!-- 自定義攔截器給webservice放行 -->
自定義攔截器:
package cn.com.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;
public class LetCxFGoFilter extends StrutsPrepareAndExecuteFilter {
public void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
//如果路徑包含cxf則放行,路徑自己選擇填寫。
if(request.getRequestURI().contains("ewebservice")||request.getRequestURI().contains("stservice")){
arg2.doFilter(arg0, arg1);
}else{
super.doFilter(arg0,arg1, arg2);
}
}}
(2)web.xml程式載入引入CXF
<filter>
<filter-name>struts2</filter-name>
<filter-class>cn.com.filter.LetCxFGoFilter</filter-class>
<init-param>
<param-name>conifg</param-name>
<param-value>
struts-plugin.xml,
struts-default.xml
</param-value>
</init-param>
</filter>
<servlet>
<servlet-name>bService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bService</servlet-name>
<!--配置訪問地址-->
<url-pattern>/ewebservice/*</url-pattern>
</servlet-mapping>
4.需要暴露的介面類用@webservice註解,方法用@webMethod。
程式碼
@WebService
public class storageShIm implements storageHService {
//注意由於是Spring採用自動注入,客戶端呼叫的時候是用的new 一個類,會報空指標錯誤,所以
將注入的介面static修飾,程式啟動就注入.
private static IStorageHistoryService shs;
public void setShs(IStorageHistoryService shs) {
this.shs = shs;
}
@WebMethod
public int getT(String s) {
System.out.println(">>>>>>>>>service>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<"+shs.test(s)+"");
return shs.test(s);
}
}
二、客戶端實現。
1.啟動服務端程式,瀏覽器輸入地址http://localhost:8080/YYY/ewebservice。選擇需要呼叫的介面方法。點選得到wsdl地址
http://localhost:8080/YYY/ewebservice/s2?wsdl。
2.新建工程使用Myeclipse自動生成客戶端程式碼,右鍵工程選擇other,新建webservice client,將wsdl地址輸入,finish即可,客戶端程式碼產生好了。寫測試類呼叫方法
public class tttt {
public static void main(String[] args) throws RemoteException,
ServiceException {
StorageHServiceProxy sp=new StorageHServiceProxy();
System.out.println(sp.getT("4"));
}
}