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> |