依赖的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
public interface HelloWorldServiceInf {
String sayHello(@WebParam(name="username") String username);
}
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface 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")
public class Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return "Hello,"+username;
}
public static void 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")
public class Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return "Hello,"+username;
}
public static void 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;
public class Client {
public static void 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;
public class Client {
public static void 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")
public class Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return "Hello,"+username;
}
public static void 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")
public class Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return "Hello,"+username;
}
public static void 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;
public class Client {
//注意:此处http://interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" />
private static final QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称
private static final QName PORT_NAME=new QName("http://interfaces.cxf.com/", "HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口类的名称+Port
public static void 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;
public class Client {
//注意:此处http://interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces.cxf.com/" />
private static final QName SERVICE_NAME=new QName("http://interfaces.cxf.com/","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称
private static final QName PORT_NAME=new QName("http://interfaces.cxf.com/", "HelloWorldServiceInfPort");//HelloWorldServiceInfPort 接口类的名称+Port
public static void 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;
public class ClientFromWsdl {
public static void 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;
public class ClientFromWsdl {
public static void 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发布和调用WebService之HelloWorld入门详解 #### 一、概述 本文将详细介绍如何使用Apache CXF框架来实现一个简单的WebService——HelloWorld示例。此示例不仅适用于初学者理解WebService的基本概念,...
本篇文章将详细介绍如何使用CXF和camel-cxf调用Web服务,以及这两个工具的核心功能和使用场景。 Apache CXF是一个全面的服务开发框架,它支持多种Web服务标准,如SOAP、WS-*协议栈、RESTful服务等。CXF提供了丰富的...
用camel-cxf调用webservice和发布一个webservice接口例子,首先启动QueryServiceMain主函数启动webservice接口,然后启动测试类TestWebservice。例子主要是实现java代码实现camel调用webservice接口
用CXF作为客户端调用webService的demo:本人亲测可用,eclipse工程项目包含完整代码和完整jar包, 只要用eclipse导入项目即可,运行控制台显示success或者false字符串,说明OK。
通过这个项目,开发者不仅可以了解SpringBoot和CXF的基本概念,还能掌握两者如何协同工作,发布和调用Web服务。同时,对于SpringBoot应用的打包、部署和测试也有了一定的认识。这个例子是一个理想的实践项目,对于...
通过Spring配置,可以实现服务的自动发布和生命周期管理。 8. **安全与认证**:CXF支持多种安全机制,包括基本认证、digest认证、WS-Security等,确保Web服务的安全性。开发者可以根据需要添加相应的安全配置。 9....
在Java开发中,Apache CXF是一个广泛使用的开源框架,它提供了创建和消费Web服务的能力。当我们需要在客户端调用Web服务时,CXF扮演着关键角色。本篇将详细讲解如何利用CXF客户端来调用Web服务以及所需的jar包。 ...
2.用cxf开发webservice 3.这个服务端和客户端的小demo 在服务端 对外开放接口服务,然后在客户端 调用服务端的方法, 实现客户端(一个javaweb项目)对服务端(javaweb项目)方法的调用, 实际上就是发送和接收消息...
本项目“cxf+spring发布webservice和restservice”专注于利用Apache CXF框架与Spring框架结合,实现这两种服务的发布。Apache CXF是一个开源的、功能丰富的服务栈,它使得开发者能够轻松地构建和部署SOAP和RESTful ...
Java WebService CXF客户端调用和服务端的实现是企业级应用程序中常见的通信方式,它基于标准的SOAP(Simple Object Access Protocol)协议,提供了一种在分布式环境中交换信息的方法。CXF是一个开源框架,它简化了...
当我们谈论“使用CXF发布WebService”时,我们实际上是在讨论如何利用Apache CXF框架创建和部署Web服务。Apache CXF是一个开源的Java框架,专门用于构建和消费Web服务,支持多种协议,如SOAP和RESTful。 首先,我们...
本篇文章将深入探讨如何使用CXF的`EndpointImpl`类来发布一个WebService,并加入拦截器。 首先,我们要理解`EndpointImpl`是CXF框架用于构建和配置Web服务端点的核心类。通过实例化这个类,我们可以自定义服务的...
本教程将详细讲解如何使用Apache CXF框架在Java环境中调用C#编写的Web服务。 **CXF简介** Apache CXF是一个开源的Java框架,用于构建和服务导向架构(SOA)。它支持多种Web服务标准,如SOAP、RESTful API等,同时也...
使用spring集成cxf,在两个web project里发布及调用webservice server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外...
在本文中,我们将深入探讨如何使用Apache CXF框架来发布和调用接口,特别是针对一个有趣的实例——星座运势接口。Apache CXF是一个流行的开源框架,它允许开发人员创建和使用Web服务,支持多种协议和标准,如SOAP、...
它支持SOAP、RESTful等多种Web服务标准,并且提供了丰富的API和工具,使得开发者可以方便地实现服务的创建、发布和调用。 在Web服务领域,WS-*(如SOAP、WSDL、UDDI)是一套用于构建企业级服务的协议栈,而RESTful ...
C#动态调用CXF WEBSERVICE框架共通类。
使用cxf webservice时容易出现的异常
本教程将详细介绍如何利用Spring Boot与CXF进行集成,以发布Web服务并实现用户和密码验证的客户端调用。 首先,我们需要在Spring Boot项目中引入CXF的依赖。这通常通过在`pom.xml`文件中添加对应的Maven依赖来完成...
学习这些内容后,开发者将能够熟练地使用CXF框架和Spring框架来构建和发布高质量的Web服务,同时也能掌握服务的测试、安全性和事务管理等关键环节。通过实践,你可以更好地理解和掌握这些技术,提升你的Web服务开发...