spring如何集成cxf實(shí)現(xiàn)webservice接口功能詳解
前言
由于cxf的web項(xiàng)目已經(jīng)集成了Spring,所以cxf的服務(wù)類都是在spring的配置文件中完成的。以下是步驟:
第一步:建立一個(gè)web項(xiàng)目。
第二步:準(zhǔn)備所有jar包。將cxf_home\lib項(xiàng)目下的所有jar包全部copy到新項(xiàng)目的lib目錄下,里面已經(jīng)包含了spring3.0的jar包。
第三步:在web.xml中配置cxf的核心servlet,CXFServlet。
第四步:創(chuàng)建(最好是Copy)cxf-servlet.xml文件。這是一個(gè)spring的配置文件。
1、web.xml中配置servlet

如果沒有提供給cxf默認(rèn)的/WEB-INF/cxf-servlet.xml配置文件,則必須要在spring的配置文件中配置以下三行配置(import)。否則將不能加載名稱為cxf的bean.從而啟動(dòng)失敗。
2、applicationContext.xml
<!--spring發(fā)布webservice服務(wù)配置 --> <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" /> <!-- 注入webservice服務(wù) --> <!-- 統(tǒng)一工號管理接口 --> <bean id="UnifiedNoServiceBean" class="com.webservice.unifiedno.service.impl.UnifiedNoServiceImpl" /> <jaxws:server id="UnifiedNoService" serviceClass="com.webservice.unifiedno.service.UnifiedNoService" address="/unifiedNoService"> <jaxws:serviceBean> <ref bean="UnifiedNoServiceBean" /> </jaxws:serviceBean> </jaxws:server>
注意:
1、<import>的三個(gè)文件是否需要全部引入根據(jù)cxf框架版本不同而不同
2、address的值為webservice注解的值: @WebService(serviceName = "unifiedNoService")
3、必須要在聲明為serviceClass的接口上聲明@WebSerive注解,因?yàn)?,要使用了接口,在接口上添加的注解才會有效?br />
4、serviceClass:必須為一個(gè)接口,并在接口上必須使用@WebService注解否則調(diào)用時(shí)會拋出異常
5、serviceBean:是實(shí)際服務(wù)的類,必須是serviceClass的子類,此類上面即可以使用@WebService注解,也可以不使用
6、address:訪問地址,省去前面的ip:port,注意在此注冊的類,必須要添加@WebService的注解
3、寫接口及實(shí)現(xiàn)類

啟動(dòng)項(xiàng)目,測試cxf是否配置成功:
訪問:http://localhost:8080/ins/services,會列出所有已經(jīng)發(fā)布的webservice接口服務(wù)

4、測試
http://localhost:8080/ins/services/unifiedNoService?wsdl

Java項(xiàng)目代碼調(diào)用服務(wù):
使用純Java項(xiàng)目調(diào)用
1、根據(jù)客戶端生成的代碼來調(diào)用。(優(yōu)選這種方式)
請先生成然后在任意的Java項(xiàng)目中調(diào)用 。
2、客戶端只擁有一個(gè)接口,使用JaxWsProxyFactoryBean來調(diào)用。
因?yàn)橐韵率褂昧薐axWsProxyFactoryBean,所以,仍然需要CXF的環(huán)境,而使用此環(huán)境就會造成Jar文件的大量冗余,所以大家要謹(jǐn)慎選擇。
1、注意,此處所說的是在Java項(xiàng)目中調(diào)用Spring的服務(wù),并不是在JavaEE項(xiàng)目中調(diào)用。后期將會講到如何在JavaEE項(xiàng)目中調(diào)用。
2、建議從wsdl地址獲取接口文件,也僅需要接口文件。
JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();
client.setAddress("http://localhost:7777/xcxf2_web/ws/one");
client.setServiceClass(IOneService.class);
IOneService one = client.create(IOneService.class);
String ss = one.sayHi("OK你好");
System.err.println(ss);
在Spring項(xiàng)目中,通過配置文件調(diào)用:
以下是使用Spring的配置文件調(diào)用:
新建立一個(gè)Java項(xiàng)目,并加載cxf的所有包。
只需要生成的接口文件。
在classpath下新建立一個(gè)ClientBeans.xml文件.
優(yōu)點(diǎn)與缺點(diǎn):
此種情況,適合于一個(gè)Javaweb項(xiàng)目已經(jīng)集成了Spring。并希望通過CXF配置的方式調(diào)用Web服務(wù)。
此種情況,仍然需要導(dǎo)入CXF的大量jar包。
這種情況也存在一定人優(yōu)點(diǎn),如可以將外部的Web服務(wù)通過配置文件注入(DI)到Action類中。
說明:
通過<jaxws:client/>來獲取WebService,id就不用說了吧。
address是不包含?wsdl的服務(wù)地址。
serviceClass是本地的接口名,與服務(wù)接口名保持相同才可以。
1、以下是ClientBeans.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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<jaxws:client id="helloClient"
address="http://127.0.0.1:9999/cxf2.4_spring_web/ws/helloworld"
serviceClass="com.itcast.cxf.first.IHelloWorld">
</jaxws:client>
</beans>
1、以下是CxfJavaClient.java的源代碼:
package com.itcast.cxfweb.java.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itcast.cxf.first.IHelloWorld;
/**
* Java項(xiàng)目的客戶端
* @author wangjianme
*/
public class CxfJavaClient {
public static void main(String[] args) {
//讀取配置文件
ApplicationContext ctx =
new ClassPathXmlApplicationContext("ClientBeans.xml");
//get到接口類型并調(diào)用
IHelloWorld hello = (IHelloWorld)ctx.getBean("helloClient");
String str = hello.sayHello("WJ");
System.err.println(str);
}
}
在本域使用jquery訪問: --查詢所有用戶:
<script type="text/javascript">
$(function(){
$("#btn1").click(function(){
var url = "http://localhost:7777/ws2/ws/user";
var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" '+
'xmlns:q0="http://service.ws2.itcast.cn/" '+
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" '+
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<soapenv:Body><q0:getUsers/></soapenv:Body></soapenv:Envelope>';
$.ajax({
url:url,//訪問的url
dataType:'xml',//返回的數(shù)據(jù)類型
type:'post',//請求方式
contentType:'application/soap+xml;charset=UTF-8',
data:soap,//數(shù)據(jù)
success:function(data,status,xhr){
//對返回后的數(shù)據(jù)進(jìn)行解析
$(data).find("return").each(function(){
var nm = $(this).find("name").text();
var age = $(this).find("age").text();
alert(nm+","+age);
});
},
error:function(xhr,status){
alert("出錯(cuò)了:"+status);
}
});
});
});
</script>
向服務(wù)器保存用戶:
以下是jsclient.jsp的源代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<script type="text/javascript"
src="<c:url value='/js/jquery-1.5.js'/>"></script>
</head>
<body>
<label for="name">姓名:</label>
<input type="text" id="name" name="name"/>
<br/>
<a href="#" id="ok">確定</a>
</body>
<script type="text/javascript">
$(function(){
$("#ok").click(function(){
var val = $("#name").val();
if($.trim(val)==""){
alert("請輸入名稱");
return;
}
var str = '<?xml version="1.0" encoding="UTF-8"?>'+
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
'<soap:Body><ns2:sayHello xmlns:ns2="http://first.cxf.itcast.com/">'+
'<arg0>'+val+'</arg0>'+
'</ns2:sayHello></soap:Body></soap:Envelope>';
$.ajax({
contentType:'application/xml;charset="UTF-8"',
dataType:'xml',
type:'post',
url:'http://localhost:9999/cxf2.4_spring_web/ws/helloworld', //直接發(fā)向這個(gè)地址
data:str,
success:function(data){
//$(data).find("return").each(function(){
// alert($(this).text());
//}); //使用上面的方法也是可以的
var ss = $(data).find("return").first().text();
$("<div>").html(ss)
.css("border","1px solid blue")
.css({width:'50%'}).
appendTo($("body"));
$("#name").val("");
}
},"xml");
});
});
</script>
</html>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
SpringMVC用JsonSerialize日期轉(zhuǎn)換方法
下面小編就為大家?guī)硪黄猄pringMVC用JsonSerialize日期轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起 小編過來看看吧2016-11-11
SpringBoot+RabbitMQ實(shí)現(xiàn)消息可靠傳輸詳解
消息的可靠傳輸是面試必問的問題之一,保證消息的可靠傳輸主要在生產(chǎn)端開啟?comfirm?模式,RabbitMQ?開啟持久化,消費(fèi)端關(guān)閉自動(dòng)?ack?模式。本文將詳解SpringBoot整合RabbitMQ如何實(shí)現(xiàn)消息可靠傳輸,需要的可以參考一下2022-05-05
Spring詳細(xì)講解FactoryBean接口的使用
這篇文章主要為大家介紹了Spring容器FactoryBean工廠實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Java數(shù)據(jù)結(jié)構(gòu)中圖的進(jìn)階詳解
在Java學(xué)習(xí)與應(yīng)用中,數(shù)據(jù)結(jié)構(gòu)無疑是每個(gè)人都要接觸的難點(diǎn),為了更好的學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)這一塊內(nèi)容,用圖來理解便是最好的方式,讓我們一起來了解本篇內(nèi)容圖的進(jìn)階2022-01-01
Java設(shè)計(jì)模式之Adapter適配器模式
這篇文章主要為大家詳細(xì)介紹了Java設(shè)計(jì)模式之Adapter適配器模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Java中線程的等待與喚醒_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
在Object.java中,定義了wait(), notify()和notifyAll()等接口。wait()的作用是讓當(dāng)前線程進(jìn)入等待狀態(tài),同時(shí),wait()也會讓當(dāng)前線程釋放它所持有的鎖。下面通過本文給大家介紹Java中線程的等待與喚醒知識,感興趣的朋友一起看看吧2017-05-05
Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用靜態(tài)工廠方法創(chuàng)建Bean操作,結(jié)合實(shí)例形式分析了靜態(tài)工廠方法創(chuàng)建Bean的相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下2019-11-11

