好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

如果没有提供给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、写接口及实现类

启动项目,测试cxf是否配置成功:

访问:http://localhost:8080/ins/services,会列出所有已经发布的webservice接口服务

4、测试

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

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

查看更多关于spring如何集成cxf实现webservice接口功能详解的详细内容...

  阅读:49次