一、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; 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)<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:/-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:(用戶端調用代碼)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
解決方法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;
}
}