第一步:在myeclipse中新建一个web项目名为myWs,
并导入依赖的jar包(cxf,spring,apache-commons相关)
1、commons-logging-1.1.1.jar
2、cxf-2.4.1.jar
3、geronimo-activation_1.1_spec-1.1.jar
4、geronimo-annotation_1.0_spec-1.1.1.jar
5、geronimo-javamail_1.4_spec-1.7.1.jar
6、geronimo-jaxws_2.2_spec-1.0.jar
7、geronimo-servlet_3.0_spec-1.0.jar
8、geronimo-stax-api_1.0_spec-1.0.1.jar
9、geronimo-ws-metadata_2.0_spec-1.1.3.jar
10、jettison-1.3.jar
11、jetty-continuation-7.4.2.v20110526.jar
12、jetty-http-7.4.2.v20110526.jar
13、jetty-io-7.4.2.v20110526.jar
14、jetty-server-7.4.2.v20110526.jar
15、jetty-util-7.4.2.v20110526.jar
16、neethi-3.0.0.jar
17、saaj-api-1.3.jar
18、saaj-impl-1.3.2.jar
19、serializer-2.7.1.jar
cxf结合spring时所需jar包,此例子也需要这些,用到了spring上下文加载
(
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring-aop-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
)
20、wsdl4j-1.6.2.jar
21、xalan-2.7.1.jar
22、xercesImpl.jar
23、xml-resolver-1.2.jar
24、xmlschema-core-2.0.jar
第二步:在WEB-INF中创建基本的cxf-beans.xml内容如下(作用:主要做webservice接口属性配置,通过web.xml配置加载,文件名和位置可以顺便,web.xml配置会用到)
<?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:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire="byType" default-lazy-init="true">
<description>使用Apache CXF的Web Service配置文件,以下三个为固定配置文件(不需要创建)</description>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<!--在这里配置相应内容-->
</beans>
第三部:在web.xml中加入cxf相应配置,内容如下:
<!--cxf start-->
<!--用于加载cxf-beans.xml配置信息-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-beans.xml</param-value>
</context-param>
<!--使用spring ContextLoaderListener 加载cxf-beans.xml-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置CXFServlet-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-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可自定义配置,用于CXFServlet请求地址拦截,访问会用到 -->
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--cxf end -->
第四步:创建webservice接口及实现类
1,创建接口
@WebService()//此注解必须,里面name,portName,targetNamespace serviceName 可以不用指定
public interface IHelloService {
public String sayHello();
}
2,创建实现类
public class HelloWorldServiceImpl implements IHelloService {
public String sayHello() {
System.out.println("It's my first webservice !");
return "你已经成功调用sayHello()方法!";
}
}
第五步:配置cxf-beans.xml,加入以下内容:
<!--id:随意配,implementor:指定接口具体实现类,address:随意配,访问时会用到,下面会做说明-->
<jaxws:endpoint id="HelloWorldService" implementor="com.ws.HelloWorldServiceImpl"
address="/IHelloService">
</jaxws:endpoint>
第六步:发布webservice到tomcat
1,验证ws是否发布成功:
1.1,如果项目发布放在TOMCAT_HOME/webapps下时:
访问http://IP地址:端口/项目名/services/{cxf-beans.xml中配置的address}?wsdl
1.2,如果是在tomcat_home/conf的server.xml中配置的是外部站点(<Context path="/testWs" docBase="D:\Developer\myWs" debug="0" reloadable="false" />),
那访问方式应该是http://IP:端口/path值(没有就不用加)/services/{cxf-beans.xml中配置的address}?wsdl,
例如:http://localhost:8080/testWs/services/IHelloService?wsdl //services对应web.xml中的<url-pattern>/services/*</url-pattern> services
验证是否能正常看到xml格式的页面
可以用SoapUi 软件测试接口
GOOD LUCKY!!!
写的不明的地方,还请大家提出
远程具体调用方式请参考:CXF 入门之远程接口调用
分享到:
相关推荐
CXF视频:1、使用CXF实现简单的HelloWorld
NULL 博文链接:https://hbiao68.iteye.com/blog/1968185
CXF源码:CXF_Spring源码 CXF源码:CXF_Spring源码
使用CXF实现简单HelloWorld.swf 主要是让初学者学习cxf使用
CXF入门的经典项目,SOA实现技术之一。要学SOA,必看。
apache cxf 一个helloworld的例子
CXF视频:2、WSDL结构分析 CXF视频:2、WSDL结构分析
使用CXF发布和调用webservice之HelloWorld入门使用CXF发布和调用webservice之HelloWorld入门
NULL 博文链接:https://ryxxlong.iteye.com/blog/1504896
使用apache cxf开发一个入门小程序
用框架cxf调用axis2提供的接口服务的一个简单例子,
本示例只是一个cxf的简单的一个helloWorld程序,运行时先运行webServiceApp.java,再在地址栏中输入http://localhost:8080/helloWorld,如果在浏览器中有一串xml输出则成功,然后再运行HelloWorldClient.java文件...
CXF入门 -- 第一个简单webService文章对应的代码;原文链接http://blog.csdn.net/neareast/article/details/7714778
NULL 博文链接:https://fengfan2008.iteye.com/blog/509342
CXF_HelloWorld,入门实践,生成wsdl文件。
apache-cxf-2.4.6.zip
cxf入门例子
CXF视频教程1 使用CXF实现简单的HelloWorld
CXF 框架实战代码---服务器端发布WebServices接口。服务器端发布WebServices接口
cxf实用案例代码,helloworld的基本代码,请参阅