本文主要介绍在Eclipse[3.3.2]安装CXF插件。开发一个简单的java web service,以及和Spring的集成。
安装插件:
1,下载STP all_in_one,从http://ftp.daum.net/eclipse/stp/old-downloads-page/可以下载stp-all-in-one-incubation-0.7.0.200711162004.zip
安装这个插件,可以用link文件的方式安装,可参考 http://blog.csdn.net/kkdelta/archive/2009/03/12/3983999.aspx
2,下载CXF 运行环境所需的Jar包,
http://people.apache.org/repo/m2-snapshot-repository/org/apache/cxf/apache-cxf/
我用的是 apache-cxf-2.1-incubator-20080414.232258-49.zip
3,打开eclipse后,在菜单栏,windows-->preference-->soa tools 如下图,说明插件安装成功。
4,配置CXF运行环境,如下图,installed Runtimes---- Add--Appach CXF 2.0--Next 指定解压缩后的apache-cxf-2.1-incubator-20080414.232258-49.zip的路径。
开发Web Java Service
1,新建一个web project,在 这个project里建立下面的interface:
package com.test.cxf;
public interface WSprovider {
public String testWS(String msgIn);
}
然后在这个建好后的project上点右键,JAX-WS Tools ---Enable JAX-WS --java first programing mode, 选择运行cxf环境-- 选择新建的interface--finish。
你的interface将被加上Java anotation如下:
package com.test.cxf;
import javax.jws.WebService;
@WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")
public interface WSprovider {
public String testWS(String msgIn);
}
2,在outline视图,选中testws(),右键选JAX-WX tools--〉create web method
你的interface将被加上Java anotation如下:
@WebService(name="WSprovider", targetNamespace="http://cxf.test.com/")
public interface WSprovider {
@WebMethod(operationName="testWS", exclude=false)
@ResponseWrapper(className="com.test.cxf.TestWSResponse", localName="testWSResponse", targetNamespace="http://cxf.test.com/")
@RequestWrapper(className="com.test.cxf.TestWS", localName="testWS", targetNamespace="http://cxf.test.com/")
public String testWS(String msgIn);
}
3,然后在project上点右键,JAX-WS Tools ---Generate All
会生成interface的实现类如下:
@WebService(endpointInterface = "com.test.cxf.WSprovider", serviceName = "WSproviderService")
public class WSproviderImpl implements WSprovider {
public java.lang.String testWS(java.lang.String arg0) {
........
}
}
到此,简单的web service的开发就算完成了。
集成Spring
1,在WEB-INF下建立一个bean.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"
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 name="testService" class="com.test.cxf.WSCXFProviderImpl"/>
<jaxws:endpoint
id="testEndpoint"
implementor="#testService"
wsdlLocation="classpath:wsdl/prjCXFWeb.wsdl"
address="/WSCXFProviderPort">
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature"/>
</jaxws:features>
</jaxws:endpoint>
</beans>
2,对生成的wsdl文件修改:
把
<soap:address location="http://localhost:9090/WSproviderPort"/>
改成
<soap:address location="http://localhost:8080/prjCXFWeb/services/WSCXFProviderPort" />
在你的src下新建一个wsdl文件,把改后的wsdl copy到此【为了对应bean.xml中的wsdlLocation】。
3,在web.xml中加入:
<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>/services/*</url-pattern>
</servlet-mapping>
4,将web project发布到web container(e.g tomcat)中,web service便可以被调用了。
在IE中输入 http://localhost:8080/prjCXFWeb/services/WSCXFProviderPort?wsdl,能看到wsdl文件,证明
web service发布成功了。
如果不和Spring集成,可以自己实现一个CXFNonSpringServlet的servlet,在web.xml中配置这个servlet来处理web service的请求.
public class SimpleServlet extends CXFNonSpringServlet {
private static final long serialVersionUID = 1L;
public void loadBus(ServletConfig servletConfig) throws ServletException {
super.loadBus(servletConfig);
BusFactory.setDefaultBus(getBus());
Object implementor = new WSCXFProviderImpl();
Endpoint.publish("/p1", implementor);
}
}
<!-- not using Spring -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
com.bt.cxf.ws.SimpleServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
在IE中输入 http://localhost:8080/prjCXFWeb/services/p1?wsdl,能看到wsdl文件,证明
web service发布成功了。[p1对应 ndpoint.publish("/p1", implementor);]
分享到:
相关推荐
用CXF+Eclipse开发Web Services程序,包括环境搭建,插件安装以及简单的Web Services入门程序。
webService实现 cxf2.7.5 与 spring3.0集成 完整流程,内涵全部jar ,下载集合运行
傻瓜试记录开发步骤,具体实现自己去完成。 如果看不懂,找自已原因。
详细描述了在Eclipse环境下,使用CXF开发并在Tomcat中部署Web服务的步骤及注意事项,均是具体实验后的总结,适合初学者参考
CXF Spring Web Service 程序,希望对大家有用!!
本示例用cxf2.6.1+spring3.1.0+hibernate3.6 开发web services . 包括客户端是java project, cxf_010 服务器端是个web程序cxf_web_010. 其中客户端的com.dx.client 包下的代码是通过wsdl2java的命令生成,复制到...
Spring CXF Mybatis 发布 Web Service 实例
完整的代码详解,让您更快的上手 jar包代码都是测试过的客户端和服务端
JAVA7和JAVA8对应CXF资源 WebService CXF 用了一天时间找,官网打不开,国内要积分,下下来又永不了。最后终于搞到手,上传上来分享给大家。 jdk版本 CXF版本 java 9及以上 3.3.x java 8 3.x java 7 2.2x --- ...
将解压后 apache-cxf-2.6.1 文件夹 lib 文件夹中的 JAR 文件导入工程下 WebContent/WEB-INF/lib 中,或直接复制到相应文件夹下,刷新工程。
使用最新的CXF2.7和Spring3的 Java WebService 集成用例 应用Spring Annotation
Spring集成Cxf调用WebServices
Apache CXF Web Service Development
Web Service学习-CXF开发Web Service实例demo(一)
Web项目中基于Maven与Spring整合的WebService之cxf的实现⬇️ 详情请参考如下链接: https://locqi.github.io/locqi.com/2018/09/05/Eclipse+Maven+Spring+CXF-create-WebService/
Spring MVC、CXF、Web Service
Spring Web Service是Spring社区基于Spring提供的一个关注于创建”文档驱动”的Web Service的模块, 它的主要目标是方便基于”契约优先”(Contract-First)的SOAP服务的开发. 好像没有多少人讨论, 大多数的话题都是...
本java工程实现了spring+cxf快速开发webservice jar包齐全导入myeclipse可直接二次开发
CXF集成Spring发布WebService代码实例
将cxf集成到spring中。用到的工具eclipse3.3 maven2