繼上一篇webService入門之后,http://www.cnblogs.com/xiaochangwei/p/4969448.html ,現在我將我周六在家研究的結果公布出來
本次集成是基於之前已經搭建好的SpringMVC+mybatis+shiro的基礎上進行的,看似很簡單的集成,但是由於jar包沖突,搞了好久,犧牲周六休息時間奉上結果
代碼文章末尾會給出下載地址的,請大家不用擔心,這些都是我搭建的,還不是很完善,要用於商業項目請修改完善。
步驟:
① 首先在 http://cxf.apache.org/download.html 下載最新版本的CXF,我下載的是3.1.6
② 由於不maven項目,所以拷貝下載的cxf lib目錄下的所有包到項目的lib路徑下。(我在這里嘗試一個個添加,后來jar包沖突了,改天優化下,找出最精簡的jar包)
③ 寫接口文件和接口的實現文件
接口文件如下,需要注意的是,接口文件上要添加@WebService注解,否則待會兒接口發布后,有這個服務,卻沒有可供調用的接口方法(自己各種條件下的嘗試,不對請拍磚)
package com.xiaochangwei.web.service; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public interface WebserviceTest { public String getUserByName(@WebParam(name = "username") String username); public void setUser(String username); public boolean getuser(String name, String password); public boolean test3(); }
接口實現類如下,經測試,接口實現類不需要添加@WebService注解,當然添加了也沒報錯
package com.xiaochangwei.web.service.impl; import com.xiaochangwei.web.service.WebserviceTest; public class WebServiceTestImpl implements WebserviceTest { @Override public String getUserByName(String username) { return "Hello:" + username; } @Override public void setUser(String username) { System.out.println("username:" + username); } @Override public boolean getuser(String name, String password) { return false; } @Override public boolean test3() { return false; } }
④ 修改web.xml文件,在末尾增加filter配置
<!-- cxf服務啟動servlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webService/*</url-pattern> </servlet-mapping>
⑤ 在Spring配置文件,默認為applicationContext.xml中增加下列代碼,需要注意的是:請加入xsd信息 注意加大加粗的字
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- 引入CXF配置文件,低版本還需引入其他兩個文件 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- 配置方式1 注意:serviceClass為接口類並非實現類 --> <jaxws:server serviceClass="com.xiaochangwei.web.service.WebserviceTest" address="/webSerciceTest_service_jaxws"></jaxws:server> <!-- 配置方式2 注意:implementor為接口的具體實現類 --> <jaxws:endpoint implementor="com.xiaochangwei.web.service.impl.WebServiceTestImpl" address="/webSerciceTest_endpoint" ></jaxws:endpoint>
上面我提供了兩種配置方式,請留意注意事項,
serviceClass : 配置的為接口定義類
implementor : 配置的為接口的實現類,且接口實現類上面不需要增加@WebService注解標簽
⑥ 啟動tomcat並測試,啟動時留意控制台可以查看配置是否可供調用。
我的啟動日志如下:
可以看到我們以兩種方式配置的發布地址都成功了,現在打開瀏覽器進行測試,請注意后面加上?wsdl
另一個和上面這個一樣,就不再截圖了。
歡迎共勉,不對請拍磚,謝謝!
源碼我近期整理好后上傳到網盤再貼上下載地址,整理好后的源碼可以直接用於商業項目開發,屆時歡迎各位架構師指點下小弟
未整理的源碼下載:http://pan.baidu.com/s/1eSuIQxs (請勿用於商業項目,若需使用請完善,整理好的代碼近期會更新上去)
下一篇我將發布SpringMVC下,怎么根據別人發布了的webService wsdl來進行調用
ps: wsdl 全稱是 web service description language 接口的描述,如果你看到這里還不知道wsdl是什么意思,請受小弟一拜 o(∩_∩)o 哈哈