CXF(2.7.10) - A simple JAX-WS service
1. 下载 apache-cxf-x.x.x.zip,在工程导入依赖的 jar 包。也可以基于 Maven 构建工程。
2. 定义服务接口。
package com.huey.demo.ws; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface HelloService { public String sayHello(@WebParam(name="who") String who); }
3. 实现服务接口。
package com.huey.demo.ws.impl; import javax.jws.WebParam; import javax.jws.WebService; import com.huey.demo.ws.HelloService; @WebService(name="helloService", endpointInterface="com.huey.demo.ws.HelloService") public class HelloServiceImpl implements HelloService { public String sayHello(@WebParam(name="who") String who) { String helloMsg = "Hello, " + who + "!"; return helloMsg; } }
4. 发布服务。
package com.huey.demo.main; import javax.xml.ws.Endpoint; import com.huey.demo.ws.HelloService; import com.huey.demo.ws.impl.HelloServiceImpl; public class Server { public void publishWebService() { HelloService helloService = new HelloServiceImpl(); String address = "http://localhost:9090/hello"; Endpoint.publish(address, helloService); } public static void main(String[] args) { try { new Server().publishWebService(); System.out.println("Server Ready..."); Thread.sleep(5 * 60 * 1000); System.out.println("Server Existing..."); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("Ending..."); } } }
5. 在浏览器键入 http://localhost:9090/hello?wsdl,验证服务是否发布成功。
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.ws.demo.huey.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://ws.demo.huey.com/" name="HelloServiceImplService" targetNamespace="http://impl.ws.demo.huey.com/"> <wsdl:import namespace="http://ws.demo.huey.com/" location="http://localhost:9090/hello?wsdl=HelloService.wsdl"/> <wsdl:binding name="HelloServiceImplServiceSoapBinding" type="ns1:HelloService"> <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="HelloServiceImplService"> <wsdl:port name="helloServicePort" binding="tns:HelloServiceImplServiceSoapBinding"> <soap:address location="http://localhost:9090/hello"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
6. 访问服务。
package com.huey.demo.main; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.huey.demo.ws.HelloService; public class Client { public static void main(String[] args) { try { System.out.println("Starting Client..."); JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloService.class); factory.setAddress("http://localhost:9090/hello"); HelloService helloService = (HelloService) factory.create(); String helloMsg = helloService.sayHello("Huey"); System.out.println(helloMsg); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("Ending..."); } } }
7. SoupUI 测试。