WebService—CXF整合Spring實現接口發布和調用過程2
一、CXF整合Spring實現接口發布
發布過程如下:
1、引入jar包(基於maven管理)
<!-- cxf --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.18</version> </dependency> <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.18</version>
</dependency>
會將cxf需要的jar都引入進來,最好是將全部jar都引入。
2、 創建接口(用於向客戶端暴露服務)
創建接口
package com.elgin.cxf.service; import javax.jws.WebParam; import javax.jws.WebService;import com.elgin.cxf.entities.User;
@WebService public interface HelloService { public String sayHello(@WebParam(name="text")String text); public User getUserByName(String name); }
接口實現類:
package com.elgin.cxf.service.impl; import javax.jws.WebService; importcom.elgin.cxf.entities.User; import com.elgin.cxf.service.HelloService; @WebService(endpointInterface="com.elgin.cxf.service.HelloService") public class HelloServiceImpl implements HelloService { @Override public String sayHello(String text ) { return "hello " + text; } @Override public User getUserByName(String name) { User user=new User(name); return user; }
}
POJO類:
package com.elgin.cxf.entities;
public class User { private String name; public String getName() { return name; }
public void setName(String name) { this.name = name; }
public User(){}
public User(String name) { super(); this.name = name; }
@Override public String toString() { return "User [name=" + name + "]"; }
}
3、 在工程的web.xml文件中增加支持spring與CXF的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>CXFWebDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 加入CXF支持 -->
<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- 加入spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
4、 增加CXF的配置文件來發布WebService接口
4.1、直接新建CXF的xml配置文件的方式
新建包CXFConfig 用來保存CXF相關的配置文件, 並在此包下新建xml配置文件 CXFServices.xml 。具體配置內容如下:
<?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:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd"> <!-- 1.使用jaxws:endpoint標簽的配置發布一個 webservice 2.服務提供實現類為 com.elgin.cxf.service.impl.HelloServiceImpl,用implementor屬性配置 3.address屬性配置外部訪問的相對路徑 4.使用 jaxws:inInterceptors 標簽配置2個日誌攔截器,用來打印調用時的日誌信息 5.註意:在此配置文件中,需加入jaxws與soap命名空間 --> <jaxws:endpoint id="helloService" implementor="com.elgin.cxf.service.impl.HelloServiceImpl" address="/hello" > <jaxws:inInterceptors > <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> </jaxws:inInterceptors> </jaxws:endpoint> </beans>
將cxf的xml文件引入Spring的xml文件中:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 用import標簽引入cxf的xml配置文件--> <import resource="/CXFConfig/CXFServices.xml"/> <!-- 項目中其它bean配置 --> <!-- .... --> </beans>
4.2、將CXF配置文件引入Spring的配置文件中
(1) cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml三個文件都在cxf-2.0.7.jar中把它們拷貝到META-INF/目錄下。將cxf的配置文件拷貝出來,定制修改,再引入Spring的xml中。在2.4版本進行了修改,2.4版本以後,只需要引入classpath:META-INF/cxf/cxf.xml.即可。
(2)<beans>元素裏面的命名空間一定要正確,特別要增加xmlns:jaxws="http://cxf.apache.org/jaxws",http://cxf.apache.org/jaxws,http://cxf.apache.org/schemas/jaxws.xsd。
<?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:p="http://www.springframework.org/schema/p" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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" /><bean id="hello" class="cn.com.service.HelloWorldImpl"/><jaxws:endpoint id="helloWorld" implementor="#hello" address="/hello" />
至此,CXF的相關配置完成,將項目加載到Tomcat ,並啟動 ,訪問 如下URL:http://localhost:8080/CXFWebDemo/services/hello?wsdl ,出現xml數據,說明發布成功。
二、CXF整合Spring實現接口調用
調用過程如下:
1、使用wsdl2Java工具手動生成本地服務端接口代碼調用
A : 引入cxf的jar包(用maven管理jar包),生成本地代碼。生成java代碼後可以直接復制到客戶端中再客戶端中使用,也可打成jar包(建議)。
用wsdl2Java工具手動生成服務端接口代碼,(首先配置cxf的環境變量,配置CXF_HOME 值為E:\gavin\cxf\apache-cxf-3.0.0,在PATH中加入%CXF_HOME%\bin(也可以直接進到bin目錄下再使用命令)。方法如下:
D:\personal\apache-cxf-3.2.0\bin>
wsdl2java -d D:/ http://169.254.123.204:8080/CXFSpring/HelloWorld?wsdl
-d D:/ 表示生成的文件放在D盤下。空格之後跟上wsdl的url。(服務器必須開啟,url才能連接上)
wsdl2java -p com -d src -all wsdl 命令說明:
-p 指定其wsdl的命名空間,也就是要生成代碼的包名
-d 指定要產生代碼所在目錄
-client 生成客戶端測試web service的代碼
-server 生成服務器啟動web service的代碼
-impl 生成web service的實現代碼
-ant 生成build.xml文件
-all 生成所有開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
補充:
1::可以用eclipse自帶的方式生成客戶端代碼
2::可以通過soapUI等接口調試工具生成客戶端代碼(需要cxf的jar包,並配置cxf的環境變量)
B : 在Spring的xml配置文件中管理客戶端調用接口的Bean組件.
<?xml version="1.0" encoding="UTF-8"?> <xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 客戶端調用配置 --> <!-- service bean配置 --> <bean id="helloService" class="com.elgin.cxf.service.HelloService" factory-bean="clientFactory" factory-method="create"/> <!-- client 工廠 ,用來產生service實例 --> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <property name="serviceClass" value="com.elgin.cxf.service.HelloService"/> <property name="address" value="http://localhost:8080/CXFWebDemo/services/hello"></property> </bean>
<!--另外一種jaxws:client方式 -->
<!-- <jaxws:client id="clientFactory"
serviceClass="com.elgin.cxf.service.HelloService"
address="http://localhost:8080/CXFWebDemo/services/hello" />-->
</beans>
說明:
1:上述的配置文件中,指定了一個bean工廠:org.apache.cxf.jaxws.JaxWsProxyFactoryBean ,該工廠可以通過 serviceClass中的 值來產生對應的服務端接口 service bean
2:同時 address 屬性,指定了webservice服務的調用地址 。註意:<property name="address" value="http://localhost:8080/webws/HelloWorld" />中的/HelloWorld要與服務器端applicationContext.xml中的
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address屬性對應。
3:同時我們註意到,有一個類:com.elgin.cxf.service.HelloService ,其實這個類就是服務端的Helloservice接口。在客戶端生成的代碼中。
C:在controller層調用
@Controller @RequestMapping("webServiceTest") public class WebServiceTestController { @Autowired private HelloService helloService; @RequestMapping("test") public @ResponseBody String test(){ return helloService.sayHello("xiaochangwei");
}
}
2、用JaxWsDynamicClientFactory創建動態的客戶端
這種方法調用的優點就是不需要使用wsdl2java來生成服務端的代碼。
新建測試類:
public class ClientDynamic { public static void main(String[] args) { method2(); } public static void method2() { //創建 JaxWsDynamicClientFactory 工廠實例 JaxWsDynamicClientFactory factory=JaxWsDynamicClientFactory.newInstance(); //根據服務地址創建客戶端 Client client=factory.createClient("http://localhost:8080/CXFWebDemo/services/hello?wsdl"); Object [] result; try { result=client.invoke("sayHello", "World"); System.out.println(result[0]); result=client.invoke("getUserByName", "Jack"); User user=(User) result[0]; System.out.println(user); } catch (Exception e) { e.printStackTrace(); } }
}
動態調用常見的問題 1:
錯誤產生的原因:
JaxWsDynamicClientFactory是一個動態代理類,,執行到這裏的時候需要編譯生成java類,但是JRE是指可以運行class文件,並沒有編譯的能力,所以需要修改eclipse中的編譯環境。產生的原因是沒有獲得編譯環境,也就是JRE設置的問題,需要在eclipse裏面把jre設置為jdk下的jre。打開Java的安裝路徑,發現會有2個jre目錄 ,比我我自己的2個目錄分別是:
C:\Program Files\Java\jre7
C:\Program Files\Java\jdk1.7.0_17\jre
現在需要把jre 為jdk下的jre目錄 ,也就是上面的第二個。
動態調用常見的問題 2:
對於這條錯誤 ,根據報錯信息來看 :在 http://impl.service.cxf.elgin.com/ 命名空間下沒有 sayHello 這個操作。因為CXF發布用的是業務類(HelloServiceImpl.java),那麽默認的命名空間就會是業務類所在包(路徑),而對外界暴露的則是接口類(HelloService.java),那麽對於客戶端(第三方)調用訪問時,需要按照接口類所在包(路徑)進行命名空間的定義。在上述錯誤的情況下,我們有2種方式來處理 ,一種是在客戶端調用時 ,指定接口的命名空間,另一種是在服務端發布WebService服務的時候,明確的指定命名空間,這樣就不存在不統一的問題了。
解決方法1:(客戶端調用代碼)
public static void method1(){ //創建 JaxWsDynamicClientFactory 工廠實例 JaxWsDynamicClientFactory factory=JaxWsDynamicClientFactory.newInstance(); //創建客戶端 Client client=factory.createClient("http://localhost:8080/CXFWebDemo/services/hello?wsdl"); Object [] result; QName qname; try { //根據指定的命名空間(接口類包名)、方法名新建QName對象 qname=new QName("http://service.cxf.elgin.com/", "sayHello"); result=client.invoke(qname, "World"); System.out.println(result[0]); qname=new QName("http://service.cxf.elgin.com/", "getUserByName"); result=client.invoke(qname, "Jack"); User user=(User) result[0]; System.out.println(user); } catch (Exception e) { e.printStackTrace(); } }
解決方法2:(在服務端指定命名空間,修改服務端的HelloServiceImpl 類):
ckage com.elgin.cxf.service.impl; import javax.jws.WebService; import com.elgin.cxf.entities.User; import com.elgin.cxf.service.HelloService; @WebService(endpointInterface="com.elgin.cxf.service.HelloService", targetNamespace="http://service.cxf.elgin.com/") public class HelloServiceImpl implements HelloService { @Override public String sayHello(String text ) { return "hello " + text; } @Override public User getUserByName(String name) { User user=new User(name); return user; } }
重新啟動Tomcat,查看鏈接內容:http://localhost:8080/CXFWebDemo/services/hello?wsdl ,發現命名空間已變為我們指定的名字:
至此,客戶端調用完畢!
WebService—CXF整合Spring實現接口發布和調用過程2