转自:http://www.blogjava.net/sxyx2008/archive/2010/09/15/332058.html
依赖的JAR
cxf-2.2.10.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
servlet-2_5-api.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar
创建一个普通的Java工程即可
创建webservice接口
package com.cxf.interfaces;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
publicinterface HelloWorldServiceInf {
String sayHello(@WebParam(name="username") String username);
}
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
publicinterface HelloWorldServiceInf {
String sayHello(@WebParam(name="username") String username);
}
发布和调用webservice
方法一
发布webservice
package com.cxf.impl;
import javax.jws.WebService;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.cxf.interfaces.HelloWorldServiceInf;
@WebService(endpointInterface="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
publicclass Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return"Hello,"+username;
}
publicstaticvoid main(String[] args) {
Server impl=new Server();
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:9000/hello");
factoryBean.setServiceClass(HelloWorldServiceInf.class);
factoryBean.setServiceBean(impl);
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.create();
}
}
import javax.jws.WebService;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.cxf.interfaces.HelloWorldServiceInf;
@WebService(endpointInterface="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
publicclass Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return"Hello,"+username;
}
publicstaticvoid main(String[] args) {
Server impl=new Server();
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:9000/hello");
factoryBean.setServiceClass(HelloWorldServiceInf.class);
factoryBean.setServiceBean(impl);
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.create();
}
}
wsdl描述文件
<?xml version="1.0" ?>
- <wsdl:definitions name="HelloWorldServiceInfService" targetNamespace="http://interfaces.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://interfaces.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://interfaces.cxf.com/" xmlns:tns="http://interfaces.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sayHello" type="tns:sayHello"/>
- <xsd:complexType name="sayHello">
- <xsd:sequence>
<xsd:element minOccurs="0" name="username" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"/>
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"/>
</wsdl:message>
- <wsdl:portType name="HelloWorldServiceInf">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"/>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldServiceInfServiceSoapBinding" type="tns:HelloWorldServiceInf">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
- <wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorldServiceInfService">
- <wsdl:port binding="tns:HelloWorldServiceInfServiceSoapBinding" name="HelloWorldServiceInfPort">
<soap:address location="http://localhost:9000/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
- <wsdl:definitions name="HelloWorldServiceInfService" targetNamespace="http://interfaces.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://interfaces.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://interfaces.cxf.com/" xmlns:tns="http://interfaces.cxf.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sayHello" type="tns:sayHello"/>
- <xsd:complexType name="sayHello">
- <xsd:sequence>
<xsd:element minOccurs="0" name="username" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
- <xsd:complexType name="sayHelloResponse">
- <xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"/>
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"/>
</wsdl:message>
- <wsdl:portType name="HelloWorldServiceInf">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"/>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldServiceInfServiceSoapBinding" type="tns:HelloWorldServiceInf">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
- <wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorldServiceInfService">
- <wsdl:port binding="tns:HelloWorldServiceInfServiceSoapBinding" name="HelloWorldServiceInfPort">
<soap:address location="http://localhost:9000/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端调用
package com.cxf.client;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cxf.interfaces.HelloWorldServiceInf;
publicclass Client {
publicstaticvoid main(String[] args) {
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setServiceClass(HelloWorldServiceInf.class);
factoryBean.setAddress("http://localhost:9000/hello");
HelloWorldServiceInf impl=(HelloWorldServiceInf) factoryBean.create();
System.out.println(impl.sayHello("张三"));
}
}
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cxf.interfaces.HelloWorldServiceInf;
publicclass Client {
publicstaticvoid main(String[] args) {
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setServiceClass(HelloWorldServiceInf.class);
factoryBean.setAddress("http://localhost:9000/hello");
HelloWorldServiceInf impl=(HelloWorldServiceInf) factoryBean.create();
System.out.println(impl.sayHello("张三"));
}
}
方法二
发布webservice
package com.cxf.impl;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import com.cxf.interfaces.HelloWorldServiceInf;
@WebService(endpointInterface="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
publicclass Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return"Hello,"+username;
}
publicstaticvoid main(String[] args) {
Server impl=new Server();
String address="http://localhost:9000/hello";
Endpoint.publish(address, impl);
}
}
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import com.cxf.interfaces.HelloWorldServiceInf;
@WebService(endpointInterface="com.cxf.interfaces.HelloWorldServiceInf",serviceName="helloWorldService")
publicclass Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return"Hello,"+username;
}
publicstaticvoid main(String[] args) {
Server impl=new Server();
String address="http://localhost:9000/hello";
Endpoint.publish(address, impl);
}
}
wsdl文件
<?xml version="1.0" ?>
- <wsdl:definitions name="helloWorldService" targetNamespace="http://impl.cxf.com/" xmlns:ns1="http://interfaces.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/"/>
- <wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldServiceInf">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
- <wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="helloWorldService">
- <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="ServerPort">
<soap:address location="http://localhost:9000/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
- <wsdl:definitions name="helloWorldService" targetNamespace="http://impl.cxf.com/" xmlns:ns1="http://interfaces.cxf.com/" xmlns:ns2="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://impl.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/"/>
- <wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldServiceInf">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
- <wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="helloWorldService">
- <wsdl:port binding="tns:helloWorldServiceSoapBinding" name="ServerPort">
<soap:address location="http://localhost:9000/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端调用
package com.cxf.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import com.cxf.interfaces.HelloWorldServiceInf;
publicclass Client {
//注意:此处http://interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" />
privatestaticfinal QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称
privatestaticfinal QName PORT_NAME=new QName("http://interfaces.cxf.com/", "HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口类的名称+Port
publicstaticvoid main(String[] args) {
String endPointAddress="http://localhost:9000/hello";
Service service=Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
HelloWorldServiceInf inf=service.getPort(HelloWorldServiceInf.class);
System.out.println(inf.sayHello("张三"));
}
}
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import com.cxf.interfaces.HelloWorldServiceInf;
publicclass Client {
//注意:此处http://interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" />
privatestaticfinal QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称
privatestaticfinal QName PORT_NAME=new QName("http://interfaces.cxf.com/", "HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口类的名称+Port
publicstaticvoid main(String[] args) {
String endPointAddress="http://localhost:9000/hello";
Service service=Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endPointAddress);
HelloWorldServiceInf inf=service.getPort(HelloWorldServiceInf.class);
System.out.println(inf.sayHello("张三"));
}
}
CXF根据wsdl文件动态调用WebService
package com.cxf.client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
publicclass ClientFromWsdl {
publicstaticvoid main(String[] args) throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9000/hello?wsdl");
//sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组
Object[] objects=client.invoke("sayHello", "张三");
//输出调用结果
System.out.println(objects[0].toString());
}
}
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
publicclass ClientFromWsdl {
publicstaticvoid main(String[] args) throws Exception{
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:9000/hello?wsdl");
//sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组
Object[] objects=client.invoke("sayHello", "张三");
//输出调用结果
System.out.println(objects[0].toString());
}
}
相关推荐
【标题】"CXF+Spring+Tomcat发布WebService"涉及的是使用Apache CXF框架与Spring框架结合,在Tomcat服务器上部署和消费Web服务的过程。这是一个常见的企业级应用开发场景,特别是对于实现基于SOAP协议的Web服务。...
在IT行业中,开发Web服务是常见的需求,而CXF和Spring框架的结合为开发者提供了一种高效、灵活的方式来实现Web Service。本篇将深入探讨如何利用CXF和Spring来创建、部署和消费Web Service。 CXF,全称CXF Commons ...
**WebService实现**:在CXF的帮助下,我们可以通过以下步骤实现一个WebService: 1. **定义服务接口**:首先,我们需要定义一个Java接口,包含要暴露给客户端的方法。 2. **创建服务实现**:然后,实现该接口,具体...
Apache CXF和Spring提供了丰富的扩展点,可以集成如Spring Security来控制访问权限,使用Spring AOP来处理事务,以及通过CXF的拦截器机制来实现自定义的日志、验证等功能。 总结来说,Apache CXF 2与Spring 2.5的...
这个项目"Spring+CXF+tomcat开发webservice"旨在教你如何利用这些技术搭建一个完整的Web服务环境,包括服务端和服务端客户端的实现。 **Spring** 是一个广泛使用的Java企业级应用开发框架,它提供了依赖注入(DI)...
【标题】:“cxf+spring webservice demo client” 在IT领域,Web服务是一种常见的系统间交互方式,它允许不同应用程序之间共享数据和服务。本示例是关于如何使用Apache CXF和Spring框架创建一个Web服务客户端的...
【标签】"CXF+spring WebService CXF"强调了这些组件的集成,特别是CXF作为Web服务的主要提供者,以及与Spring的紧密配合。CXF不仅可以用作服务提供者,还可以作为客户端来消费其他服务,这在Spring的管理下变得更加...
总的来说,"cxf+spring的webservice实例"是一个实践性的教程,旨在帮助开发者理解如何在Spring环境中利用CXF快速构建和部署Web服务。通过这个实例,你可以掌握从创建服务到发布、测试的整个流程,进一步提升你的Java...
【标题】"cxf+spring webservice server demo"是一个基于Apache CXF和Spring框架构建的Web服务服务器端示例项目。这个项目展示了如何将CXF与Spring集成,以创建、部署和运行一个高效的Web服务。 【描述】指出,由于...
### CXF + Spring + JDK1.5 开发 WebService 的技术要点 #### 一、概述 在本篇文章中,我们将详细介绍如何使用 CXF (Community Xenith Framework)、Spring 框架以及 JDK 1.5 来开发并部署 WebService 应用到 ...
这个例子将涵盖从创建服务接口、实现该接口、配置CXF和Spring,到最终在Tomcat服务器上运行服务的全过程。 【标签】"webservice"表明我们关注的是基于标准的、平台无关的通信方式,即Web服务。Web服务允许不同的...
本项目“cxf+spring发布webservice和restservice”专注于利用Apache CXF框架与Spring框架结合,实现这两种服务的发布。Apache CXF是一个开源的、功能丰富的服务栈,它使得开发者能够轻松地构建和部署SOAP和RESTful ...
标题“简单cxf+spring构建webservice服务”指的是使用Apache CXF框架与Spring框架结合来创建Web服务。Apache CXF是一个开源的Java框架,它允许开发者创建和消费各种Web服务,包括SOAP和RESTful风格。Spring框架则为...
在这个"CXF+Spring WebService实例"中,我们将深入探讨如何利用这两个工具来创建、发布和消费Web服务。 CXF全称为CXF CXF (CXF XFire + XWS), 是一个开源的Java框架,它支持多种Web服务标准,如SOAP、WSDL、WS-...
"CXF+Spring整合"是企业级Java应用程序中常用的一种技术组合,它结合了Apache CXF(一个用于构建和服务导向架构的开源框架)和Spring框架的优势,以实现高效、灵活的服务开发和管理。在Java世界里,CXF常用于创建Web...
Spring还提供了对Web服务的支持,可以通过集成CXF来实现Web服务的发布和消费。 在使用CXF和Spring 2.5开发Web服务时,我们需要进行以下步骤: 1. **环境准备**:确保安装了Java Development Kit (JDK) 1.6或更高...
总结来说,"webservice cxf+spring maven项目"是一个适合初学者的示例,它展示了如何利用CXF、Spring和Maven构建、部署和测试Web服务。这个项目涵盖了Web服务的基本概念,以及CXF和Spring在Web服务中的实际应用,为...
综上所述,"CXF+Spring 完整版例子"是一个全面的教程,涵盖了从基本的Web服务创建到高级功能的实现。通过深入研究这个示例,开发者可以掌握如何在实际项目中有效地使用Spring和CXF,创建高效、可靠的Web服务应用。
在结合 Spring 框架使用时,CXF 可以方便地与 Spring 集成,实现服务的发布和调用。Spring 可以管理 CXF 组件的生命周期,提供事务、安全等服务,并通过配置简化服务的实现。 **一、搭建开发环境** 1. **选择依赖...