package com.ws; import javax.jws.WebMethod; import javax.jws.WebService; /** * WebService入门学习 * WebService注解是告诉JAXWS,这是一个WebService类 * * @author 14040875 * */ @WebService public class Hello { /** * 向接收的名字问好 * WebMethod注解告诉JAXWS,此方法为soap方法 * * @param name * @return */ @WebMethod public String say(String name){ return "Hello, " + name; } }
wsgen -cp D:\dev\apache-tomcat-7.0.53\webapps\HelloWebService\WEB-INF\classes com.ws.Hello -wsdl -s wssrc -d build -r wsdl
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> <endpoint name='Hello' implementation='com.ws.Hello' url-pattern='/HelloService' /> </endpoints>
<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <span style="white-space:pre"> </span><servlet-name>Hello</servlet-name> <span style="white-space:pre"> </span><servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <span style="white-space:pre"> </span><load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <span style="white-space:pre"> </span><servlet-name>Hello</servlet-name> <span style="white-space:pre"> </span><url-pattern>/HelloService</url-pattern> </servlet-mapping> </web-app>
wsimport -keep -d bin -s src wsdl/HelloService.wsdl
package com.ws; public class HelloClient { public static void main(String[] args) { HelloService service = new HelloService(); Hello port = service.getHelloPort(); System.out.println(port.say("Mike")); } }
<?xml version="1.0" encoding="UTF-8"?> <bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="wsdl/HelloService.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws"> <bindings node="wsdl:definitions"> <enableAsyncMapping>true</enableAsyncMapping> </bindings> </bindings>
wsimport -keep –b binding.xml -d bin -s src wsdl/HelloService.wsdl
package com.ws; import javax.xml.ws.Response; public class HelloClient { public static void main(String[] args) throws Exception { HelloService service = new HelloService(); Hello port = service.getHelloPort(); Response<SayResponse> sayAsync = port.sayAsync("Mike"); while (!sayAsync.isDone()) { System.out.println("waiting..."); } try { SayResponse callNameResponse = sayAsync.get(); String message = callNameResponse.getReturn(); System.out.println(message); } catch (Exception ex) { } } }运行程序,得到如下结果:
package com.ws; import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; public class HelloClient { public static void main(String[] args) throws Exception { HelloService service = new HelloService(); Hello port = service.getHelloPort(); port.sayAsync("Mike", new AsyncHandler<SayResponse>() { public void handleResponse(Response<SayResponse> res) { try { SayResponse response = null; response = res.get(); String message = response.getReturn(); System.out.println(message); } catch (Exception e) { e.printStackTrace(); } System.out.println("Callback!"); } }); System.out.println("begin waiting..."); Thread.sleep(1000); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。