当前位置: 代码迷 >> Web前端 >> CXF 2.0 学习笔记-一(服务端)
  详细解决方案

CXF 2.0 学习笔记-一(服务端)

热度:516   发布时间:2014-01-03 14:10:51.0
CXF 2.0 学习笔记-1(服务端)

从网络上找了很多入门的例子,但大部分都是有这样或那样的问题,搞的头大。
?? 这里根据自己搭建的过程,整理出一个实际例子。
?? 环境:jdk1.5+,eclipse3.4,Tomcat5.5,cxf-2.1.4
???以下没有用到任何插件来生成
?? 服务端
?????一、server 端:?
??????????接口:WebServiceSample
?????????

import?hs.cxf.bean.ReqBean;
import?hs.cxf.bean.RespBean;
import?hs.cxf.bean.UserDTO;
import?javax.jws.WebParam;
import?javax.jws.WebResult;
import?javax.jws.WebService;

/**
?*?WebService接口定义类.
?*?
?*?使用@WebService将接口中的所有方法输出为Web?Service.?
?*?可用annotation对设置方法、参数和返回值在WSDL中的定义.
?
*/

@WebService
public?interface?WebServiceSample?{

????/**
?????*?一个简单的方法,返回一个字符串
?????*?
?????*?
@param?hello
?????*?
@return
?????
*/

????String?say(String?hello);

????/**
?????*?稍微复杂一些的方法,传递一个对象给服务端处理
?????*?
?????*?
@param?user
?????*?
@return
?????
*/

????String?sayUserName(@WebParam(name?=?"user")?UserDTO?user);

????/**
?????*?通过对象来交互
?????
*/

????@WebResult(name?=?"RespBean")
????RespBean?action(@WebParam(name?=?"ReqBean")?ReqBean?req);

}


?? 实现类:WebServiceSampleImpl
??

import?javax.jws.WebService;
import?hs.cxf.bean.ReqBean;
import?hs.cxf.bean.RespBean;
import?hs.cxf.bean.UserDTO;

/**
?*?@Title:
?*?
?*?@Description:
?*?
?*?@Copyright:?Copyright?(c)?2009?FFCS?All?Rights?Reserved
?*?
?*?
@author?zhengzhi?2009-3-16?上午11:22:23
?*?
@version?1.00.000
?*?
?
*/

@WebService(endpointInterface="hs.cxf.server.WebServiceSample")
public?class?WebServiceSampleImpl?implements?WebServiceSample?{
????public?String?sayUserName(UserDTO?user)?{
????????return?"hello?"?+?user.getName();
????}


????public?String?say(String?hello)?{

????????return?"hello?"?+?hello;
????}


????public?RespBean?action(ReqBean?req)?{
????????RespBean?respBean?=?new?RespBean();
????????respBean.setExp(req.getExp());
????????respBean.setRespId(req.getSeqId());
????????return?respBean;
????}


}


? 实体类:UserDTO,ReqBean,RespBean
?

UserDTO
@XmlAccessorType(XmlAccessType.FIELD)???
@XmlType(name?=?"User")???
public?class?UserDTO?{???
??
????protected?Integer?id;???
??
????protected?String?name;???
????//get和set方法,这里不写出
}

ReqBean
@XmlAccessorType(XmlAccessType.FIELD)???
@XmlType(name?=?"ReqBean")???
public?class?ReqBean?{
????private?String?exp;
????private?String?seqId;
???????//get和set方法,这里不写出
}


RespBean
@XmlAccessorType(XmlAccessType.FIELD)???
@XmlType(name?=?"RespBean")???
public?class?RespBean?{
????private?String?respId;
????private?String?exp;
???????//get和set方法,这里不写出
}


?二、Spring 配置
??????

<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"
>???
??
????<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="jaxWsServiceFactoryBean"??
????????class
="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">???
????????<property?name="wrapped"?value="true"?/>???
????????<property?name="dataBinding"?ref="aegisBean"?/>???
????</bean>???
??
????<bean?id="aegisBean"??
????????class
="org.apache.cxf.aegis.databinding.AegisDatabinding"?/>???
??
????<jaxws:endpoint?id="CollectiveServices"??
????????implementor
="hs.cxf.server.WebServiceSampleImpl"?address="/HelloWorld">???
????????<jaxws:serviceFactory>???
????????????<ref?bean="jaxWsServiceFactoryBean"/>???
????????</jaxws:serviceFactory>???
????</jaxws:endpoint>???
</beans>??



三.web.xml配置

<?xml?version="1.0"?encoding="UTF-8"?>
<!DOCTYPE?web-app?PUBLIC?"-//Sun?Microsystems,?Inc.//DTD?Web?Application?2.3//EN"?"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app?id="WebApp_ID">
????<display-name>cxfTest</display-name>
????<context-param>
????????<param-name>contextConfigLocation</param-name>
????????<param-value>WEB-INF/beans.xml</param-value>
????</context-param>
????<listener>
????????<listener-class>
????????????org.springframework.web.context.ContextLoaderListener???
????????</listener-class>
????</listener>
????<servlet>
????????<servlet-name>CXFServlet</servlet-name>
????????<servlet-class>
????????????org.apache.cxf.transport.servlet.CXFServlet???
????????</servlet-class>
????????<load-on-startup>1</load-on-startup>
????</servlet>
????<servlet-mapping>
????????<servlet-name>CXFServlet</servlet-name>
????????<url-pattern>/ws/*</url-pattern>
????</servlet-mapping>
</web-app>

这里要注意,cxf 教程上提到需要的包并不完整,还需要导入:jdom,stax-api-1.0-2.jar,jaxws-api.jar?这些包
?我使用的包如下:

?

aopalliance-1.0.jar
asm-2.2.3.jar
commons-logging-1.1.1.jar
cxf-2.1.4.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.3.jar
geronimo-servlet_2.5_spec-1.2.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.9.jar
jaxen-core.jar
jaxen-jdom.jar
jaxws-api.jar
jdom.jar
jetty-6.1.9.jar
jetty-util-6.1.9.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
saxpath.jar
spring-beans-2.0.8.jar
spring-context-2.0.8.jar
spring-core-2.0.8.jar
spring-web-2.0.8.jar
stax-api-1.0-2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.6.jar
xalan.jar
xml-resolver-1.2.jar
XmlSchema-1.4.3.jar


然后启动tomcat,输入http://IP:端口/模块名/ws 查看你生成的服务端,可使用xmlspy来进行测试。

  相关解决方案