CXF+MyEclipse 开发webservice入门实例之HelloWorld
2010-08-30 19:59
519 查看
按照CXF的用户文档上的描述,来写一个helloworld的webservice小实例!
第一步,新建一个名为“CXF_HelloWorld”的web project,加入如下jar包。
另外还需要加入spring的jar包。
第二步,配置web.xml,把CXFServlet配置到web.xml中
代码如下:
第三步,编写webservice
HelloWorld.java
HelloWorldImpl.java
第四步,把编写好的服务部署到cxf-servlet.xml中
cxf-servlet.xmls放在web-inf目录下
第五步,编写客户端调用webservice
客户端HelloWorldClient.java
第六步,部署,运行
把项目部署到tomcat上,启动tomcat,运行HelloWorldClient.java
控制台成功输出hello world!
第一步,新建一个名为“CXF_HelloWorld”的web project,加入如下jar包。
另外还需要加入spring的jar包。
第二步,配置web.xml,把CXFServlet配置到web.xml中
代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
第三步,编写webservice
HelloWorld.java
package cxf.server; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorld { @WebMethod public String sayHello(@WebParam String message); }
HelloWorldImpl.java
package cxf.server; public class HelloWorldImpl implements HelloWorld{ @Override public String sayHello(String message) { // TODO Auto-generated method stub return message; } }
第四步,把编写好的服务部署到cxf-servlet.xml中
cxf-servlet.xmls放在web-inf目录下
<?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:soap="http://cxf.apache.org/bindings/soap" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:server id="HelloWorld" serviceClass="cxf.server.HelloWorld"> <jaxws:serviceBean> <bean class="cxf.server.HelloWorldImpl" /> </jaxws:serviceBean> </jaxws:server> </beans>
第五步,编写客户端调用webservice
客户端HelloWorldClient.java
package cxf.client; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import cxf.server.HelloWorld; public class HelloWorld { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.getInInterceptors().add(new LoggingInInterceptor()); factory.getOutInterceptors().add(new LoggingOutInterceptor()); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/CXF_HelloWorld/services/HelloWorld"); HelloWorld client = (HelloWorld) factory.create(); String reply = client.sayHello("hello world!"); System.out.println(reply); } }
第六步,部署,运行
把项目部署到tomcat上,启动tomcat,运行HelloWorldClient.java
控制台成功输出hello world!
相关文章推荐
- CXF+MyEclipse 开发webservice入门实例之HelloWorld
- XFire+MyEclipse 开发WebService入门实例之HelloWorld
- XFire+MyEclipse 开发WebService入门实例之HelloWorld
- 应用CXF开发RESTful风格的WebService入门实例
- MyEclipse下XFire开发Webservice实例
- java使用XFire组件开发WebService入门实例
- CXF3.0.2+Spring3.2.14 WebService入门实例五(下)
- CXF入门教程(3) -- webService客户端开发步骤详解
- webservice CXF入门图解 附实例源码
- MyEclipse下XFire开发Webservice实例
- WebService学习入门(SOAP协议,ApacheCXF入门,基于CXF技术开发CRM服务器)
- 使用CXF 开发SOAP webservice 服务端实例
- CXF3.0.2+Spring3.2.14 WebService入门实例四
- MyEclipse下XFire开发Webservice实例
- CXF3.0.2+Spring3.2.14 WebService入门实例六
- 使用CXF发布和调用webservice之HelloWorld入门
- MyEclipse下用XFire开发WebService(入门级)(1)
- WebService 开发入门 基于CXF框架
- cxf+spring开发(一)--- 实现webservice简单输出实例
- MyEclipse 6 实战开发讲解视频入门 0: 下载 安装 运行 HelloWorld