以下实现了一个CXF(apache-cxf-2.7.8)的入门实例HelloWorld,供大家参考。如有不足之处,请多多包涵。
1、需要导入的jar包
2、工作空间的结构(MyEclipase8)
3、需要暴露的接口(Java代码)
package cxf.server; import javax.jws.WebService; /** * 服务接口 * @author Administrator * 2014-1-17 */ @WebService public interface IHelloCXFService { /** * 接受用户名,然后输出问好信息 * @param name * @return */ public String sayHello(String name); }
4、暴露接口的实现(Java代码)
package cxf.server; import javax.jws.WebService; /** * 服务的实现 * @author Administrator * 2014-1-17 */ @WebService(endpointInterface="cxf.server.IHelloCXFService", serviceName="helloCXF") public class IHelloCXFServiceImpl implements IHelloCXFService { public IHelloCXFServiceImpl() {} public String sayHello(String name) { return "你好!" + name; } }
5、CXF服务暴露配置文件(WebRoot/WEB-INF/cxf-servlet.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: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"> <!-- 暴露Web服务: address 服务地址 serviceClass 服务接口类 --> <jaxws:server id="helloCXFService" serviceClass="cxf.server.IHelloCXFService" address="/helloCXF"> <jaxws:serviceBean> <!-- 服务实现类 --> <bean class="cxf.server.IHelloCXFServiceImpl" /> </jaxws:serviceBean> </jaxws:server> </beans>
7、web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- CXF相关 --> <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> </web-app>
8、启动服务,验证服务是否发布成功。服务地址格式:http://主机地址:端口/服务/Web服务。
http://localhost:8080/test_cxf/services
services 实在web.xml中CXF的Servlet处配置的(/services/*)
发布服务成功界面:
9、使用Java代码调用Web服务,有一下三种方式:
package cxf.client; import javax.xml.namespace.QName; import javax.xml.ws.Service; import javax.xml.ws.soap.SOAPBinding; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import cxf.server.IHelloCXFService; /** * CXF客户端调用 * @author Administrator * 2014-1-20 */ public class HelloCXFClient { /* * 服务名称 * http://server.cxf/ —— 来自targetNamespace="http://server.cxf/" * IHelloCXFService —— 暴露接口名称 */ private static final QName SERVICE_NAME = new QName("http://server.cxf/", "IHelloCXFService"); /* * 服务端口 * IHelloCXFServicePort —— 暴露接口名称 + Port */ private static final QName PORT_NAME = new QName("http://server.cxf/", "IHelloCXFServicePort"); public static void main(String[] args) throws Exception { // 1. 方式一 // 服务端点地址:http://localhost:8080/test_cxf/services/helloCXF?wsdl 去掉 ?wsdl String endpointAddress = "http://localhost:8080/test_cxf/services/helloCXF"; Service service = Service.create(SERVICE_NAME); service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); IHelloCXFService hcxf = service.getPort(IHelloCXFService.class); System.out.println(hcxf.sayHello("tom")); // 2. 方式二 // 根据wsdl文件动态调用WebService JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf .createClient("http://localhost:8080/test_cxf/services/helloCXF?wsdl"); // sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组 Object[] objects = client.invoke("sayHello", new Object[]{"Bear"}); // 输出调用结果 System.out.println(objects[0].toString()); // 3. 方式三 JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean(); factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); factoryBean.setServiceClass(IHelloCXFService.class); factoryBean.setAddress("http://localhost:8080/test_cxf/services/helloCXF"); IHelloCXFService impl=(IHelloCXFService) factoryBean.create(); System.out.println(impl.sayHello("Bluse")); } }
10、调用结果
2014-1-20 10:16:51 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://server.cxf/}IHelloCXFService from class cxf.server.IHelloCXFService
2014-1-20 10:16:52 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://server.cxf/}IHelloCXFService from class cxf.server.IHelloCXFService
你好!tom
2014-1-20 10:16:53 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames
信息: Created classes: cxf.server.ObjectFactory, cxf.server.SayHello, cxf.server.SayHelloResponse
你好!Bear
2014-1-20 10:16:53 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://server.cxf/}IHelloCXFServiceService from class cxf.server.IHelloCXFService
2014-1-20 10:16:53 org.apache.cxf.services.IHelloCXFServiceService.IHelloCXFServicePort.IHelloCXFService
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8080/test_cxf/services/helloCXF
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://server.cxf/"><arg0>Bluse</arg0></ns2:sayHello></soap:Body></soap:Envelope>
--------------------------------------
2014-1-20 10:16:53 org.apache.cxf.services.IHelloCXFServiceService.IHelloCXFServicePort.IHelloCXFService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[216], content-type=[text/xml;charset=UTF-8], Date=[Mon, 20 Jan 2014 02:16:53 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse xmlns:ns2="http://server.cxf/"><return>你好!Bluse</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>
--------------------------------------
你好!Bluse
项目源码:Test_CXF.zip 压缩文件
更多IT技术请浏览:www.hxstrive.com
相关推荐
cxf入门实例(webservice),帮你更好的入门
CXF_HelloWorld,入门实践,生成wsdl文件。
NULL 博文链接:https://spacecity.iteye.com/blog/1474400
学习CXF WebService入门实例一.pdf学习CXF WebService入门实例一.pdf
CXF 入门实例 CXF对Interceptor拦截器的支持 传递复杂类型对象 CXF整合Spring
用cxf2.2.4版本写的一个小例子,如果用eclipse开发的话, 可以直接导入便可运行. 下载cxf把jar导入到项目的lib下
CXF实例源代码 客户端调用 web service 入门教程
CXF实例源代码(服务器端) 入门教程 webservice 实例
包含服务器端和客户端,自己写的,能成功的运行,
一个用springboot搭建的简单的cxf实例,可以用于入门跟学习
cxf开发的基础入门简介与实现,可以帮助普通刚刚入门的java研发者一些关于cxf的介绍
一个CXF的入门简单小程序,里面包含着用到的库以及所有代码。
利用CXF3.0.2+Spring3.2.14传输文件,里面有笔者在练习过程中遇到的错误,还有源代码可以下载!
cxf的简易开发说明,采用maven搭建,spring管理cxf,代码内配置详细注释,相信能帮到新学的朋友。
使用apache cxf开发一个入门小程序
用CXF+Eclipse开发Web Services程序,包括环境搭建,插件安装以及简单的Web Services入门程序。
包含CXF开发所需要的jar包和入门实例代码!
环境: bpel: Apache ODE ws: Apache CXF(这里是两个控制台程序,需要cxf库。也可自已根据wsdl写web service)
Java+WebService利用(cxf)开发笔记,里面有非常好的学习实例,是入门学生的非常好的资料