从网络上找了很多入门的例子,但大部分都是有这样或那样的问题,搞的头大。
这里根据自己搭建的过程,整理出一个实际例子。
环境: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);
}
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;
}
}
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方法,这里不写出
}
@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>
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>
<!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
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来进行测试。
相关推荐
赠送jar包:cxf-rt-rs-client-3.0.1.jar; 赠送原API文档:cxf-rt-rs-client-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-client-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-rs-client-3.0.1.pom; 包含...
1. **库文件**:包含了CXF运行所需的JAR文件,如cxf-api.jar、cxf-rt-core.jar等,这些文件需要添加到项目的类路径中。 2. **文档**:可能包含API文档、用户指南和开发者手册,帮助开发者理解和使用CXF。 3. **示例*...
赠送jar包:cxf-rt-frontend-jaxrs-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxrs-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxrs-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxrs...
赠送jar包:cxf-rt-transports-http-3.0.1.jar; 赠送原API文档:cxf-rt-transports-http-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-transports-http-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-transports-...
赠送jar包:cxf-rt-frontend-simple-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-simple-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-simple-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-...
赠送jar包:cxf-rt-frontend-jaxws-3.0.1.jar; 赠送原API文档:cxf-rt-frontend-jaxws-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-frontend-jaxws-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-frontend-jaxws...
赠送jar包:cxf-rt-bindings-soap-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-soap-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-soap-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-soap-...
赠送jar包:cxf-core-3.0.1.jar; 赠送原API文档:cxf-core-3.0.1-javadoc.jar; 赠送源代码:cxf-core-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-core-3.0.1.pom; 包含翻译后的API文档:cxf-core-3.0.1-...
cxf-core-3.0.0.jar,cxf-rt-bindings-soap-3.0.0.jar,cxf-rt-databinding-jaxb-3.0.0.jar,cxf-rt-frontend-jaxws-3.0.0.jar,cxf-rt-frontend-simple-3.0.0.jar,cxf-rt-transports-http-3.0.0.jar,cxf-rt-...
cxf-rt-frontend-jaxws-3.0.16.jar jar包下载3.0.16版本下载
总之,"apache-camel-2.0-M1-src.zip"是了解和研究Apache Camel 2.0 Milestone 1源代码的入口,它包含了Camel的组件、DSL、路由和依赖管理等多个关键方面,对于学习和扩展Camel功能的开发者来说,是一个宝贵的资源。...
赠送jar包:cxf-rt-databinding-jaxb-3.0.1.jar; 赠送原API文档:cxf-rt-databinding-jaxb-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-databinding-jaxb-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-...
赠送jar包:cxf-rt-rs-extension-providers-3.0.1.jar; 赠送原API文档:cxf-rt-rs-extension-providers-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-rs-extension-providers-3.0.1-sources.jar; 赠送Maven依赖信息...
赠送jar包:cxf-rt-bindings-xml-3.0.1.jar; 赠送原API文档:cxf-rt-bindings-xml-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-bindings-xml-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-bindings-xml-3.0.1....
赠送jar包:cxf-rt-ws-addr-3.0.1.jar; 赠送原API文档:cxf-rt-ws-addr-3.0.1-javadoc.jar; 赠送源代码:cxf-rt-ws-addr-3.0.1-sources.jar; 赠送Maven依赖信息文件:cxf-rt-ws-addr-3.0.1.pom; 包含翻译后的API...
标题 "cxf-rt-transports-http-jetty-3.0.2.zip" 指的是Apache CXF项目的一个特定版本的HTTP-Jetty传输模块。Apache CXF是一个流行的开源框架,用于构建和开发服务导向架构(SOA)和RESTful Web服务。这个版本,...
【压缩包子文件的文件名称列表】仅包含一个文件名:“cxf-1”。这可能是一个文件夹或者一个具体的文档,如PDF、HTML或JAR,包含了CXF框架的教程、示例代码、API文档或者是其他学习资源。通常,这样的资源会包括以下...
<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"/>
此外,还有其他如`cxf-rt-bindings-soap.jar`、`cxf-rt-databinding-jaxb.jar`、`cxf-rt-rs-extension-providers.jar`等,它们分别对应于不同的功能,如SOAP绑定、JAXB数据绑定和RESTful服务的提供者扩展。...
cxf-dosgi-ri-singlebundle-distribution-1.4.0.jar