依赖的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入门使用CXF发布和调用webservice之HelloWorld入门
springboot整合CXF发布webservice和客户端调用 项目中每个类都有详细的注释,保证都能看懂,不失为一个学习springboot整合cxf来学习webservice发布调用的好例子
用camel-cxf调用webservice和发布一个webservice接口例子,首先启动QueryServiceMain主函数启动webservice接口,然后启动测试类TestWebservice。例子主要是实现java代码实现camel调用webservice接口
用CXF作为客户端调用webService的demo:本人亲测可用,eclipse工程项目包含完整代码和完整jar包, 只要用eclipse导入项目即可,运行控制台显示success或者false字符串,说明OK。
cxf客户端调用webservice所需jar包
2.用cxf开发webservice 3.这个服务端和客户端的小demo 在服务端 对外开放接口服务,然后在客户端 调用服务端的方法, 实现客户端(一个javaweb项目)对服务端(javaweb项目)方法的调用, 实际上就是发送和接收消息...
java语言实现使用spring+mybatis+oracle访问数据库,使用cxf发布webservice,并使用axis调用webservice。项目使用maven管理jar包。
现在主流的webService框架,一个是axis2,一个是cxf,本资源是基于cxf框架的简单例子
MyCXFService为WebService服务端,MyCXFClient为客户端调用 ...7、以上第4点为利用Web形式进行调用WebService,还有其它方法调用: 手动执行com.lun.test.Test.java与Test1.java里的main方法,也是调用WebService服务
NULL 博文链接:https://liugang594.iteye.com/blog/2022626
使用spring集成cxf,在两个web project里发布及调用webservice server端使用spring+springmvc+mybatis+cxf,client端使用struts2+spring+hibernate+cxf 两个工程均为myeclipse project,包含所有除myeclipse自带以外...
使用cxf webservice时容易出现的异常
java调用c#的webservice,其中包括demo和需要的jar包,下载下来,导入eclipse就可以直接看到main方法。不需要配置什么文件
【精品文档】基于cxf webservice传递List及bean.pdf CXF学习笔记.doc 使用Apache CXF开发Web Service.pdf 如何使用myeclipse开发 webservice
本项目主要是使用cxf框架发布服务和调用服务的例子,调用的是星座运势接口。
Java 调用webservice cxf客户端调用demo和服务端,包含jar包,服务端发布后直接用客户端调用
cxf3.2.0作为服务端或客户端,发布webservice或者调用webservice所需的最少jar包。
1. 发布和调用WebService: 使用CXF2.4(http://cxf.apache.org)和spring 2. 调用安全性: 使用简单的USERNAME_TOKEN 3. 服务程序中取得调用者身份 ------------------------- 接口 ------------------------- intf....
Springboot整合CXF发布Web service和客户端调用(用户和密码验证)打开及用
纯java调用ws-security+CXF实现的webservice安全接口