以下实现了一个CXF(apache-cxf-2.7.8)的入门实例HelloWorld,供大家参考。如有不足之处,请多多包涵。
1、需要导入的jar包
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.bug315.com