目錄
一、服務(wù)端
1、依賴
1.1 第一種方式
1.2 第二種方式
2、測試代碼
3、配置文件
3.1 第一種配置方式
3.2 第二種配置方式
4、運(yùn)行
二、生成客戶端
第一種方式wsdl2java
第二種方式 wsimport
三、測試
工作上遇到了,所以自己琢磨了一下,也是第一次想到用博客來記錄,平時(shí)也有用word文檔記錄,怕忘了。所以在這里來一個(gè)詳細(xì)總結(jié),那些關(guān)于WebService和CXF什么的我這里就不多說了,那些網(wǎng)上一搜一大把,就直接上干貨。
這里自己琢磨出兩種方式,一種是引入依賴,另一種是下載apache-cxf的二進(jìn)制文件解壓縮,在eclipse里配置好,這樣就不要引入依賴了,在apache-cxf/lib目錄下有所有關(guān)于CXF的jar包和Spring相關(guān)的jar包,可以自己下載以后去看,如果還需引入其他jar包,另外在pom中添加依賴,下載后記得配置環(huán)境變量,先說第一種。
一、服務(wù)端
建立Maven項(xiàng)目,引入依賴
1、依賴
1.1 第一種方式
直接在pom文件里添加依賴。
<groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId>
CXF只需添加這三個(gè)jar包的依賴,Maven會(huì)自動(dòng)引入幫我們引入其他jar包。在pom文件里添加完成后保存,就會(huì)觸發(fā)Maven自動(dòng)去下載你添加的依賴jar包(如果你本地倉庫有就直接是取的本地倉庫的) ,然后項(xiàng)目目錄下就會(huì)有Maven Dependencies,如下圖,是添加完成后的。

在pom文件中我只添加了三個(gè)依賴jar包,其他這么多jar包就是Maven自動(dòng)為我添加的。和spring整合發(fā)布webservice還要加入spring的依賴jar包,這個(gè)就自己去添加了,第一種就說完了。
1.2 第二種方式
把下載的二進(jìn)制文件解壓縮到一個(gè)目錄,然后配置環(huán)境變量:
1、變量名:CXF_HOME 值:apache-cxf的解壓縮路徑,示例:E:\Install software\apache-cxf-3.2.5
2、在path后面加上 %CXF_HOME%/bin;
在cmd命令中輸入wsdl2java,如果有提示usage,就表明配置成功。
apache-cxf二進(jìn)制文件下載地址:http://cxf./download.html
下載箭頭所指的那個(gè):

再就是建立項(xiàng)目,不過不需要引入依賴了,只要在eclipse里配置了,如圖:

在項(xiàng)目上右鍵,Build Path / Configure Build Path / Add library / CXF Runtime,然后選擇apache-cxf,點(diǎn)擊Finish就好了,你的項(xiàng)目目錄下就會(huì)多出一個(gè)Apache CXF Library,其他的就跟下面一樣了。
不過這種方法在運(yùn)行的時(shí)候可能會(huì)隔段時(shí)間就會(huì)報(bào)錯(cuò),但是服務(wù)還是能正常運(yùn)行,下面是報(bào)錯(cuò)信息:
DefaultValidationEventHandler: [ERROR]: prefix wsdp is not bound to a namespace Location: node: [wsd:Types: null]javax.xml.bind.UnmarshalException: prefix wsdp is not bound to a namespace- with linked exception: [java.lang.IllegalArgumentException: prefix wsdp is not bound to a namespace]
到網(wǎng)上搜索報(bào)錯(cuò)原因,是因?yàn)閘ib目錄下有多余jar包導(dǎo)致的,解決方案是把多余的jar包刪除。
- cxf-services-ws-discovery-api-3.1.4.jar
- services-ws-discovery-service-3.1.4.jar
- services-wsn-api-3.1.4.jar
- services-wsn-core-3.1.4.jar
- manifest.jar
其中,MANIFEST.MF文件在這個(gè)目錄下..\apache-cxf-3.2.5\samples\jax_rs\minimal_osgi\src\main\resources\META-INF。
2、測試代碼
創(chuàng)建一個(gè)接口,記得加上@WebService注解,表示你要“暴露”的接口(服務(wù)類)。
public interface HelloService { public String sayHello(String name) ;
實(shí)現(xiàn)類:
//實(shí)現(xiàn)類上可以不添加@Webservice注解 public class HelloServiceImp implements HelloService { public String sayHello(String name) {
3、配置文件
配置方式的話也有兩種,先來看下第一種方式。
3.1 第一種配置方式
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd"> <import resource="spring-cxf.xml" />
spring-cxf.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xmlns:jaxws="http://cxf./jaxws" <!-- 別忘記添加命名空間 --> xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd http://cxf./schemas/jaxws.xsd"> <!--其中id是自己定義的,implementor是接口實(shí)現(xiàn)類,address就是訪問的地址 --> <!-- 相當(dāng)于Endpoint.publish("http://localhost:8080/service", newHelloServiceImp()); --> <jaxws:endpoint id="helloService" implementor="com.eastcom.ws.impl.HelloServiceImp" address="/hello"/>
3.2 第二種配置方式
spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd"> <bean id="HelloService" class="com.eastcom.ws.impl.HelloServiceImp"> <import resource="spring-cxf.xml" />
spring-cxf.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xmlns:jaxws="http://cxf./jaxws" <!-- 別忘記添加命名空間 --> xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans-3.0.xsd http://www./schema/context http://www./schema/context/spring-context-3.0.xsd http://cxf./schemas/jaxws.xsd"> <!-- implementor屬性里寫的是bean的id,但是要以#開頭,特定寫法 --> <jaxws:endpoint implementor="#HelloService" address="/hello"/>
補(bǔ)充:
1、至于以前還需引入的 cxf.xml 和 cxf-servlet.xml,網(wǎng)上說是cxf3.0以后就不需要了,至于為什么,原因在這里。
<import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
2、關(guān)于spring-cxf.xml中另一種配置方式(這種方式我沒嘗試,有興趣的下伙伴可以自己去試下)。
<!--相當(dāng)于:JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); --> <jaxws:server address="/hello" serviceClass="com.eastcom.ws.impl.HelloServiceImp"> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
上面配置完了,該到web.xml文件里配置了。
<param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> <servlet-name>cxf</servlet-name> <url-pattern>/service/*</url-pattern>
4、運(yùn)行
配置完了以后,部署到Tomcat上運(yùn)行,右鍵run as/run on server/Finish,Eclipse彈出下圖所示界面表示服務(wù)已經(jīng)成功發(fā)布,如果彈出404或者報(bào)錯(cuò),就是發(fā)布失敗。當(dāng)然也可以到瀏覽器里面看運(yùn)行效果,地址是:http://localhost:8080/項(xiàng)目名/service 。點(diǎn)擊箭頭所指的地方就能看到wsdl文件。(不要在意我這里的項(xiàng)目名)


二、生成客戶端
第一種方式wsdl2java
CXF提供的根據(jù)wsdl生成客戶端代碼的命令。
在cmd命令中輸入:wsdl2java -d 指定代碼生成目錄 -client webservice的訪問地址url
示例:wsdl2java -d E:\\AllWorkSpace\\MyWork\\TheClient\\src -client http://localhost:8080/Dom4j_AxisDemo/service/hello?wsdl
注意中間的空格?。?!
具體用法自行百度,這里只對(duì)上面的用法做解釋:
-d 指定要產(chǎn)生代碼所在目錄
-client 生成客戶端測試web service的代碼
代碼生成后如圖:

第二種方式 wsimport
JDK提供的生成客戶端的命令。
在cmd命令中輸入:wsimport -s 指定代碼生成目錄 -p 包名 -keep webservice訪問地址url
示例:wsimport -s E:\\AllWorkSpace\\MyWork\\TheClient\\src -p com.eastcom.ws.client -keep http://localhost:8080/Dom4j_AxisDemo/service/hello?wsdl
同樣注意中間的空格!?。?/p>
目錄地址中不能含有空格,發(fā)布地址不要忘了?wsdl

三、測試
代碼
public class TestService { public static void main(String[] args) { HelloService service=new HelloServiceImpService().getHelloServiceImpPort(); System.out.println(service.sayHello("CXF"));
結(jié)果

第一次寫,也當(dāng)做一次學(xué)習(xí)經(jīng)歷,以上就是我自己總結(jié)的內(nèi)容,中間有我遇到的問題,以及解決方案,有引用到別人的經(jīng)驗(yàn)。
|