WebService—CXF整合Spring實現介面釋出和呼叫過程
CXF整合Spring實現介面釋出
釋出過程如下:
1、引入jar包(基於maven管理)
<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; import com.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)元素裡面的名稱空間一定要正確,特別要增加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服務的呼叫地址 。注意:中的/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 類):
```1
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 ,發現名稱空間已變為我們指定的名字:
至此,客戶端呼叫完畢!