1.首先創建一個Web services EndPoint:html
Java代碼java
package Hello; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.Endpoint; @WebService public class Hello { @WebMethod public String hello(String name) { return "Hello, " + name + "\n"; } public static void main(String[] args) { // create and publish an endpoint Hello hello = new Hello(); Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello); } } package Hello; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.Endpoint; @WebService public class Hello { @WebMethod public String hello(String name) { return "Hello, " + name + "\n"; } public static void main(String[] args) { // create and publish an endpoint Hello hello = new Hello(); Endpoint endpoint = Endpoint.publish("http://localhost:8080/hello", hello); } } |
2.使用 apt 編譯 Hello.java(例:apt -d [存放編譯後的文件目錄] Hello.java ) ,會生成 jaws目錄web
3.使用java Hello.Hello運行,而後將瀏覽器指向http://localhost:8080/hello?wsdl就會出現下列顯示瀏覽器
4.使用wsimport 生成客戶端maven
使用以下:wsimport -p com.test -keep http://localhost:8080/hello?wsdlspa
5.客戶端程序:code
Java代碼server
class HelloClient{ public static void main(String args[]) { HelloService service = new HelloService(); Hello helloProxy = service.getHelloPort(); String hello = helloProxy.hello("你好"); System.out.println(hello); } } class HelloClient{ public static void main(String args[]) { HelloService service = new HelloService(); Hello helloProxy = service.getHelloPort(); String hello = helloProxy.hello("你好"); System.out.println(hello); } }xml
ant wsgen 生成 web serverhtm
<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="maven.compile.classpath"/> <target name="wsgen"> <wsgen outputDirectory="${basedir}/target/generated-source" wsdl="https://localhost/test.asmx?wsdl" package="com.test.ws" overwrite="true"/> </target> |