依赖的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工程即可
package
com.cxf.interfaces;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorldServiceInf {
String sayHello(@WebParam(name = " username " ) String username);
}
发布和调用webservice
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorldServiceInf {
String sayHello(@WebParam(name = " username " ) String username);
}
方法一
发布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 " )
class Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return " Hello, " + username;
}
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描述文件
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 " )
class Server implements HelloWorldServiceInf {
public String sayHello(String username) {
return " Hello, " + username;
}
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();
}
}
<?
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 xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
- wsdl:types xsd:schema attributeFormDefault ="unqualified" elementFormDefault >
xsd:element ="sayHello" type ="tns:sayHello" />
- xsd:complexType ="sayHello" xsd:sequence minOccurs ="0" name ="username" ="xsd:string" />
</ xsd:complexType ="sayHelloResponse" ="tns:sayHelloResponse" ="sayHelloResponse" ="return" xsd:schema wsdl:message wsdl:part element ="parameters" wsdl:message wsdl:portType ="HelloWorldServiceInf" wsdl:operation wsdl:input message wsdl:output wsdl:operation wsdl:portType wsdl:binding ="HelloWorldServiceInfServiceSoapBinding" ="tns:HelloWorldServiceInf" soap:binding style ="document" transport ="http://schemas.xmlsoap.org/soap/http" soap:operation soapAction ="" style soap:body use ="literal" wsdl:input wsdl:output wsdl:binding wsdl:service ="HelloWorldServiceInfService" wsdl:port binding ="tns:HelloWorldServiceInfServiceSoapBinding" ="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 xmlns:wsdl ="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
- wsdl:types xsd:schema attributeFormDefault ="unqualified" elementFormDefault >
xsd:element ="sayHello" type ="tns:sayHello" />
- xsd:complexType ="sayHello" xsd:sequence minOccurs ="0" name ="username" ="xsd:string" />
</ xsd:complexType ="sayHelloResponse" ="tns:sayHelloResponse" ="sayHelloResponse" ="return" xsd:schema wsdl:message wsdl:part element ="parameters" wsdl:message wsdl:portType ="HelloWorldServiceInf" wsdl:operation wsdl:input message wsdl:output wsdl:operation wsdl:portType wsdl:binding ="HelloWorldServiceInfServiceSoapBinding" ="tns:HelloWorldServiceInf" soap:binding style ="document" transport ="http://schemas.xmlsoap.org/soap/http" soap:operation soapAction ="" style soap:body use ="literal" wsdl:input wsdl:output wsdl:binding wsdl:service ="HelloWorldServiceInfService" wsdl:port binding ="tns:HelloWorldServiceInfServiceSoapBinding" ="HelloWorldServiceInfPort" soap:address location ="http://localhost:9000/hello" wsdl:port wsdl:service wsdl:deFinitions >
package
com.cxf.client;
import org.apache.cxf.jaxws.JaxWsProxyfactorybean;
import com.cxf.interfaces.HelloWorldServiceInf;
class Client {
void main(String[] args) {
JaxWsProxyfactorybean factorybean = new JaxWsProxyfactorybean();
factorybean.getininterceptors().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.jaxws.JaxWsProxyfactorybean;
import com.cxf.interfaces.HelloWorldServiceInf;
class Client {
void main(String[] args) {
JaxWsProxyfactorybean factorybean = new JaxWsProxyfactorybean();
factorybean.getininterceptors().add( new LoggingOutInterceptor());
factorybean.setServiceClass(HelloWorldServiceInf. class );
factorybean.setAddress( " http://localhost:9000/hello " );
HelloWorldServiceInf impl = (HelloWorldServiceInf) factorybean.create();
System.out.println(impl.sayHello( " 张三 " ));
}
}
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
" + username;
}
new Server();
String address = " http://localhost:9000/hello " ;
Endpoint.publish(address, impl);
}
}
="helloWorldService"
="http://impl.cxf.com/"
xmlns:ns2
wsdl:import
="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl"
namespace
="helloWorldServiceSoapBinding"
="ns1:HelloWorldServiceInf"
="helloWorldService"
="tns:helloWorldServiceSoapBinding"
="ServerPort"
import
javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.soAPBinding;
// 注意:此处http: interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location=" http://localhost :9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace=" http://interfaces.cxf.com/ " />
private final QName SERVICE_NAME = new QName( " http://interfaces.cxf.com/ " , " HelloWorldServiceInf " ); HelloWorldServiceInf接口类的名称
final QName PORT_NAME = " HelloWorldServiceInfPort " ); HelloWorldServiceInfPort 接口类的名称+Port 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
import javax.xml.ws.Service;
import javax.xml.ws.soap.soAPBinding;
// 注意:此处http: interfaces.cxf.com/ 来源于wsdl文件中namespace <wsdl:import location=" http://localhost :9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace=" http://interfaces.cxf.com/ " />
private final QName SERVICE_NAME = new QName( " http://interfaces.cxf.com/ " , " HelloWorldServiceInf " ); HelloWorldServiceInf接口类的名称
final QName PORT_NAME = " HelloWorldServiceInfPort " ); HelloWorldServiceInfPort 接口类的名称+Port 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
org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicclientFactory;
class ClientFromWsdl {
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()); } }
class ClientFromWsdl {
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()); } }