激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring如何集成cxf實現webservice接口功能詳解

spring如何集成cxf實現webservice接口功能詳解

2021-05-19 13:17架構師小跟班 Java教程

這篇文章主要給大家介紹了關于spring如何集成cxf實現webservice接口功能的相關資料,文中通過示例代碼介紹的非常詳細,對大家 的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧

前言

由于cxf的web項目已經集成了spring,所以cxf的服務類都是在spring的配置文件中完成的。以下是步驟:

第一步:建立一個web項目。

第二步:準備所有jar包。將cxf_home\lib項目下的所有jar包全部copy到新項目的lib目錄下,里面已經包含了spring3.0的jar包。

第三步:在web.xml中配置cxf的核心servlet,cxfservlet。

第四步:創建(最好是copy)cxf-servlet.xml文件。這是一個spring的配置文件。

1、web.xml中配置servlet

spring如何集成cxf實現webservice接口功能詳解

如果沒有提供給cxf默認的/web-inf/cxf-servlet.xml配置文件,則必須要在spring的配置文件中配置以下三行配置(import)。否則將不能加載名稱為cxf的bean.從而啟動失敗。

2、applicationcontext.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!--spring發布webservice服務配置 -->
<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服務 -->
<!-- 統一工號管理接口 -->
<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>的三個文件是否需要全部引入根據cxf框架版本不同而不同

2、address的值為webservice注解的值: @webservice(servicename = "unifiednoservice")

3、必須要在聲明為serviceclass的接口上聲明@webserive注解,因為,要使用了接口,在接口上添加的注解才會有效。

4、serviceclass:必須為一個接口,并在接口上必須使用@webservice注解否則調用時會拋出異常

5、servicebean:是實際服務的類,必須是serviceclass的子類,此類上面即可以使用@webservice注解,也可以不使用

6、address:訪問地址,省去前面的ip:port,注意在此注冊的類,必須要添加@webservice的注解

3、寫接口及實現類

spring如何集成cxf實現webservice接口功能詳解

啟動項目,測試cxf是否配置成功:

訪問:http://localhost:8080/ins/services,會列出所有已經發布的webservice接口服務

spring如何集成cxf實現webservice接口功能詳解

4、測試

http://localhost:8080/ins/services/unifiednoservice?wsdl

spring如何集成cxf實現webservice接口功能詳解

java項目代碼調用服務:

使用純java項目調用

1、根據客戶端生成的代碼來調用。(優選這種方式)

請先生成然后在任意的java項目中調用 。

2、客戶端只擁有一個接口,使用jaxwsproxyfactorybean來調用。

因為以下使用了jaxwsproxyfactorybean,所以,仍然需要cxf的環境,而使用此環境就會造成jar文件的大量冗余,所以大家要謹慎選擇。

1、注意,此處所說的是在java項目中調用spring的服務,并不是在javaee項目中調用。后期將會講到如何在javaee項目中調用。

2、建議從wsdl地址獲取接口文件,也僅需要接口文件。

?
1
2
3
4
5
6
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項目中,通過配置文件調用:

以下是使用spring的配置文件調用:

新建立一個java項目,并加載cxf的所有包。

只需要生成的接口文件。

在classpath下新建立一個clientbeans.xml文件.

優點與缺點:

此種情況,適合于一個javaweb項目已經集成了spring。并希望通過cxf配置的方式調用web服務。

此種情況,仍然需要導入cxf的大量jar包。

這種情況也存在一定人優點,如可以將外部的web服務通過配置文件注入(di)到action類中。

說明:

通過<jaxws:client/>來獲取webservice,id就不用說了吧。

address是不包含?wsdl的服務地址。

serviceclass是本地的接口名,與服務接口名保持相同才可以。

1、以下是clientbeans.xml的文件的源代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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的源代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.itcast.cxfweb.java.client;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.itcast.cxf.first.ihelloworld;
/**
 * java項目的客戶端
 * @author wangjianme
 */
public class cxfjavaclient {
 public static void main(string[] args) {
  //讀取配置文件
  applicationcontext ctx =
   new classpathxmlapplicationcontext("clientbeans.xml");
  //get到接口類型并調用
  ihelloworld hello = (ihelloworld)ctx.getbean("helloclient");
  string str = hello.sayhello("wj");
  system.err.println(str);
 }
}

在本域使用jquery訪問: --查詢所有用戶:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<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',//返回的數據類型
     type:'post',//請求方式
     contenttype:'application/soap+xml;charset=utf-8',
     data:soap,//數據
     success:function(data,status,xhr){
      //對返回后的數據進行解析
      $(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("出錯了:"+status);
     }
    });
   });
  });
 </script>

向服務器保存用戶:

以下是jsclient.jsp的源代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<%@ 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',  //直接發向這個地址
    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>

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://www.cnblogs.com/xyhero/p/9348469.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区三区视频观看 | 青青草成人免费视频在线 | 麻豆视频在线免费观看 | 国产91精品久久久久久 | 国产精品久久久久久久久久久久久久久久 | 欧美日韩在线免费观看 | wwwxxx国产 | 久久精品伊人网 | 中文字幕欧美视频 | 免费在线观看午夜视频 | hd性videos意大利复古 | 久久精品久久精品国产大片 | 主播粉嫩国产在线精品 | 精品黑人一区二区三区国语馆 | 日本欧美一区二区三区在线播 | 日本aaaa片毛片免费观看视频 | 狼伊千合综网中文 | 国产资源在线看 | 九九热视频在线免费观看 | 国产精品午夜在线 | 久久精品79国产精品 | 成人免费看片a | 国产免费一级 | 久久成人综合网 | 激情小说激情图片激情电影 | 在线播放黄色网址 | 精品亚洲午夜久久久久91 | 国产另类一区 | 欧美日韩精品一区二区三区蜜桃 | 久久久久女人精品毛片 | 欧美日韩精品一区二区三区蜜桃 | 国产无限资源在线观看 | 香蕉久久久精品 | 国产成人在线一区二区 | 成年性羞羞视频免费观看无限 | 亚洲极色| 欧美久久久一区二区三区 | 中文字幕在线视频日本 | 国产成人精品二区 | 日韩视频―中文字幕 | 亚洲一区 国产精品 |