依赖的JAR package com.cxf.interfaces; 发布和调用webserviceimport javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloWorldServiceInf { String sayHello(@WebParam(name="username") String username); } 方法一 发布webservice package com.cxf.impl; 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") 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(); } } <?xml version="1.0" ?> 客户端调用- <wsdl:definitions name="HelloWorldServiceInfService" targetNamespace="http://interfaces./" xmlns:ns1="http://schemas./wsdl/soap/http" xmlns:soap="http://schemas./wsdl/soap/" xmlns:tns="http://interfaces./" xmlns:wsdl="http://schemas./wsdl/" xmlns:xsd="http://www./2001/XMLSchema"> - <wsdl:types> - <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://interfaces./" xmlns:tns="http://interfaces./" xmlns:xsd="http://www./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./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("张三")); } } 发布webservice package com.cxf.impl; wsdl文件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); } } <?xml version="1.0" ?> 客户端调用- <wsdl:definitions name="helloWorldService" targetNamespace="http://impl./" xmlns:ns1="http://interfaces./" xmlns:ns2="http://schemas./wsdl/soap/http" xmlns:soap="http://schemas./wsdl/soap/" xmlns:tns="http://impl./" xmlns:wsdl="http://schemas./wsdl/" xmlns:xsd="http://www./2001/XMLSchema"> <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces./" /> - <wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldServiceInf"> <soap:binding style="document" transport="http://schemas./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; CXF根据wsdl文件动态调用WebServiceimport 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./ 来源于wsdl文件中namespace <wsdl:import location="http://localhost:9000/hello?wsdl=HelloWorldServiceInf.wsdl" namespace="http://interfaces./" /> private static final QName SERVICE_NAME=new QName("http://interfaces./","HelloWorldServiceInf");//HelloWorldServiceInf接口类的名称 private static final QName PORT_NAME=new QName("http://interfaces./", "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("张三")); } } 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()); } } |
|
来自: CevenCheng > 《CXF》