`
天涯之海
  • 浏览: 48050 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

使用Spring+CXF开发WebService

 
阅读更多

<!-- [if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]-->

使用Spring+CXF 开发 WebService

 

Apache CXF 提供方便的 Spring 整合方法,可以通过注解、 Spring 标签式配置来暴露 Web Services 和消费 Web Services

各种类型的Annotation @WebService @WebMethod WSDL 映射 Annatotion 。这些 Annotation 将描述 Web Service WSDL 文档元素和 Java 源代码联系在一起。 @SOAPBinding 是一个绑定的 annotation 用来说明网络协议和格式。

 

1 @WebService annotation 的元素 name,serviceName targetNamespace 成员用来描述

   wsdl:portType wsdl:service ,和 targetNameSpace 生成 WebService 中的 WSDL 文件。

2 @SOAPBinding 是一个用来描述 SOAP 格式和 RPC 的协议的绑定 Annotation

3 @WebMethod Annotation operationName 成员描述了 wsdl:operation ,而且它的操作描  

   述了WSDL 文档中的 SOAPAction 头部。这是客户端必须要放入到 SQAPHeader 中的数

   值,SOAP 1.1 中的一种约束。

4 @WebParam Annotation partName 成员描述了 WSDL 文档中的 wsdl:part

5 @WebResult Annotation partName 成员描述了 wsdl:part 用来返回 WSDL 文档的值。

 

例如下面使用annotation 定义了一个 webservice

import  java.util.List;

import  javax.jws.WebMethod;

import  javax.jws.WebParam;

import  javax.jws.WebResult;

import  javax.jws.WebService;

import  com.cxf.pojo.User;

 

@WebService (targetNamespace =  "http://jdk.study.hermit.org/client" )

public   interface  UserService {

@WebMethod (operationName= "Insert" )

public   void  insert(  @WebParam (name =  "userId" ) String userid, 

    @WebParam (name =  "userName" ) String username, 

    @WebParam (name =  "userEmail" ) String useremail, 

    @WebParam (name =  "userAge" int  userage);

@WebMethod (operationName= "GetUserById" )

@WebResult (name =  "result" )

public  User getUserById( @WebParam (name= "userid" ) String userid);

@WebMethod (operationName= "GetAllUsers" )

@WebResult (name =  "result" )

public  List getAllUsers();

}

 

 

 

其实现类如下所示:

import  java.util.List;

import  javax.jws.WebService;

 

import  com.cxf.dao.UserDao;

import  com.cxf.pojo.User;

import  com.cxf.service.UserService;

 

@WebService (endpointInterface= "com.cxf.service.UserService" )

public   class  UserServiceImpl  implements  UserService {

 

private  UserDao  userDao ;

public  List getAllUsers() {

return   userDao .findAllUser();

}

 

public   User  getUserById(String userid) {

return   userDao .findUserById(userid);

}

 

public   void  insert(String userid, String username, String useremail,  int  userage) {

User user= new  User();

user.setUserage(userage);

user.setUseremail(useremail);

user.setUserid(userid);

user.setUsername(username);

userDao .insert(user);

System. out .println( "insert successfully!" );

}

 

public   void  setUserDao(UserDao userDao) {

this . userDao  = userDao;

}

}

注意:实现类中的@WebService ,其中的 endpointInterface 成员指定了该类实现的接口

 

Spring 的配置文件中,需要对其进行配置:

首先在ApplicationContext.xml(Spring 的配置文件 ) 中引入 CXF XML Scheam  配置文件 ), 如下 :

< beans  xmlns = "http://www.springframework.org/schema/beans"

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

xmlns:jaxws="http://cxf.apache.org/jaxws"

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 ">

<! 还需要引入以下3 个关于 CXF 的资源文件,这三个文件在 cxf.jar -->

< 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>

 

其次就是在 Spring的配置文件中配置 webservice ,如下所示:

< jaxws:endpoint  id = "userManager"  address = "/UserManager"

         implementorClass = "com.cxf.service.UserService" >

         < jaxws:implementor >

             < bean  id = "userServiceImpl"

                 class = "com.cxf.service.impl.UserServiceImpl" >

                 < property  name = "userDao" >

                     < ref  bean = "userDao"  />

                 </ property >

             </ bean >

         </ jaxws:implementor >

  </ jaxws:endpoint >

 

注意:

①、 address   webservice 发布的地址

②、 implementorClass   为该webservice 实现的接口

③、 < jaxws:implementor ></ jaxws:implementor > 之间定义的是 implementorClass 指定接口的实现类

 

另外,在Spring 的配置文件中配置完成后,需要修改 web.xml 文件

< servlet >

< servlet-name > CXFServlet </ servlet-name >

< servlet-class >

org.apache.cxf.transport.servlet.CXFServlet

</ servlet-class >

</ servlet >

< servlet-mapping >

< servlet-name > CXFServlet </ servlet-name >

< url-pattern > /services/* </ url-pattern >

</ servlet-mapping >

注意<url-pattern>/   services /*</url-pattern>配置, CXFServlet 会拦截此类 url ,进行处理 。上面配置的webservice 将通过如下 URL 访问到:

http://localhost:8080/cxf-ws/services/   UserManager

UserManager < jaxws:endpoint  > 标签中address 属性对应的值

cxf-ws 为本项目的名称

 

配置完成之后,将项目部署到tomcat

输入URL http://localhost:8080/cxf-ws/services/   UserManager ?wsdl

将会看到生成的wsdl 文件

 

为了能够直接访问到 com.cxf.service.UserService 中的 insert 方法,进行如下测试:

创建一个insertUser.html 页面:

< html >

< head >

< script  type = "text/javascript" >

function  post()  {

var  xmlhttp= false ;

if  (!xmlhttp &&  typeof  XMLHttpRequest!= 'undefined' {

try {

xmlhttp =  new  XMLHttpRequest();

} catch  (e)  {

xmlhttp= false ;

}

}

if  (!xmlhttp && window.createRequest)  {

try {

xmlhttp = window.createRequest();

} catch  (e)  {

xmlhttp= false ;

}

}

var  dest = document.getElementById( 'destination' ).value;

xmlhttp.open( "POST" , dest,  true );

xmlhttp.onreadystatechange= function ()  {

    if  (xmlhttp.readyState==4)  {

     document.getElementById( "result" ).innerHTML = xmlhttp.responseText;

     }

}

xmlhttp.setRequestHeader( "Man" "POST "  + dest +  " HTTP/1.1" )

xmlhttp.setRequestHeader( "MessageType" "CALL" )

xmlhttp.setRequestHeader( "Content-Type" "text/xml" );

var  texta = document.getElementById( 'texta' );

var  soapmess = texta.value;

xmlhttp.send(soapmess);

}

</ script >

</ head >

< body >

hi there

< form  id = "forma" >

< textarea  id = "texta"   rows = "10"  cols = "80" >

<?xml version='1.0' encoding='UTF-8'?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

 xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/1999/XMLSchema">

 <SOAP-ENV:Body>

 < I nsert>

  <userId>2005221104210066</userId>

  <userName>Leon Cao</userName>

  <userEmail>caohongliang2@163.com</userEmail>

  <userAge>23</userAge>

</ I nsert>

  </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

</ textarea >

< p >

< input  type = "text"  id = "destination"  size = "50"  value = " http://localhost:8080/cxf-ws/services/UserManager " ></ input >

< p >

< input  type = "button"  onclick = "post()"  name = "submit"  value = "Submit" ></ input >

</ form >

< div  id = "container" ></ div >

Result:

< hr >

< div  id = "result" >

</ div >

< hr >

< div  id = "soap" >

</ div >

</ body >

</ html >

在该测试页面需要注意标记为红色的部分,如下是一个soap message

<?xml version='1.0' encoding='UTF-8'?>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

 xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/1999/XMLSchema">

 <SOAP-ENV:Body>

 < I nsert>

  <userId>2005221104210066</userId>

  <userName>Leon Cao</userName>

  <userEmail>caohongliang2@163.com</userEmail>

  <userAge>23</userAge>

</ I nsert>

  </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

注意:

<SOAP-ENV:Body > 标签之间 < I nsert> 对应 @WebMethod (operationName= "Insert" )

  中的operationName 属性的值

<userId> <userName> <userEmail> <userAge> 标签对各自对应着insert 方法中  

  @WebParam name 属性的值:

@WebParam (name =  "userId" ) String userid, 

@WebParam (name =  "userName" ) String username, 

@WebParam (name =  "userEmail" ) String useremail, 

@WebParam (name =  "userAge" int  userage);

当向webservice 上发送该 soap 消息的时候将自动解析该 soap 消息,将 Insert 解析为 insert 方法,将 <userId> <userName> <userEmail> <userAge> 标签对之间的值解析为相对应的参数 userid username useremail userage

< input  type = "text"  id = "destination"  size = "50"  value = " http://localhost:8080/cxf-ws/services/UserManager " ></ input >

该文本标签中的value 属性为 webservice 的地址

 

运行该页面,点击Submit 后将在数据库中插入一行数据,即调用了 inser 方法成功

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics