CXF Hello World
来源:互联网 发布:视频软件 编辑:程序博客网 时间:2024/06/07 04:51
废话不多说,直接贴上CXF开发的hello world如下:
1、在eclipse中新建web工程, 并导入apache-cxf-2.4.3\lib下所有jar文件
2、编辑web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebService-CXF_Demo" version="2.5"><display-name>CXF-Demo</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:ws-server-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><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
3、在src文件夹下建立文件ws-server-beans.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:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsdhttp://cxf.apache.org/corehttp://cxf.apache.org/schemas/core.xsd"><!-- Exposing the HelloWorld service as a SOAP service --> <bean id="jaxbBean" class="org.apache.cxf.jaxb.JAXBDataBinding" scope="prototype"/> <bean id="jaxws-and-aegis-service-factory" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean" scope="prototype"> <property name="dataBinding" ref="jaxbBean"/> <property name="serviceConfigurations"> <list> <bean class="org.apache.cxf.jaxws.support.JaxWsServiceConfiguration"/> <bean class="org.apache.cxf.aegis.databinding.AegisServiceConfiguration"/> <bean class="org.apache.cxf.service.factory.DefaultServiceConfiguration"/> </list> </property> </bean><!-- Soap over HTTP--><jaxws:endpoint id="helloWorld" implementor="com.citi.crc.HelloWebServiceImpl" address="/HelloWorld" > <jaxws:serviceFactory> <ref bean="jaxws-and-aegis-service-factory"/> </jaxws:serviceFactory></jaxws:endpoint></beans>
4、创建接口
package com.citi.crc;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface IHelloWebService {@WebMethod(operationName = "sayHello2Person")public String sayHello(String name);}
5、 实现接口
package com.citi.crc;import javax.jws.WebService;@WebService(portName="HelloCXFServiceEndPort", serviceName="HelloCXFService", targetNamespace="http://crc.citi.com/HelloWebServiceImplService", endpointInterface="com.citi.crc.IHelloWebService")public class HelloWebServiceImpl implements IHelloWebService {@Overridepublic String sayHello(String name) {if (name == null || "".equals(name)) {name = "Zhang San";}return "Hello " + name;}}
6、 测试: 将web工程部署到web server 上, 访问url: http://localhost:7001/CXF-Demo/services 或者http://localhost:7001/CXF-Demo/services/HelloWorld?wsdl, 如果能访问则上述简单的web server开发部署成功。
7、 Client 调用Wer Service
package com.citi.crc.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.citi.crc.IHelloWebService;public class CXFWebServiceClient {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:client-beans.xml" });//invoke the webserviceIHelloWebService client = (IHelloWebService) context.getBean("clientOverHttp");System.out.println(client.sayHello("Jensen"));System.exit(0);}}
client-beans.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="clientOverHttp"class="com.citi.crc.HelloWebServiceImpl"factory-bean="clientFactoryHttp" factory-method="create" /><bean id="clientFactoryHttp" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass"value="com.citi.crc.IHelloWebService" /><property name="address"value="http://localhost:7001/CXF-Demo/services/HelloWorld" /></bean></beans>
结束!
- CXF Hello World
- cxf 入门(hello world)
- cxf 之 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- CXF学习之旅(二)- Hello World
- Apache CXF实战之一 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- CXF Web Service的Hello World之一 搭建环境
- Apache CXF实战之一 Hello World Web Service
- Demystifying Apache CXF: A RESTful Hello World App
- Apache CXF实战之一 Hello World Web Service
- Apache CXF实战之一 Hello World Web Service
- Hello World!【Hello World】
- Hello, world!
- android Google map 的开发误区
- 编程之美2.8查找符合条件的数
- LINUX内核随记(四)--文件系统(1)
- 使用Log4j进行日志操作(牛小浩)不错的
- 构造器陷阱(序列化恢复Java对象,clone复制Java对象,无限递归的构造器)
- CXF Hello World
- Ajax JSON
- 如何调用google地图的一个简单示例
- linux 设备模型简析
- U盘安装 debian
- JAVA中的序列化和反序列化
- gold_hash_map design
- extjs model的dirty属性能够查询store中某一个行是否改变过
- 网上搜的bonding和vlan的相关材料