Reference: http://blog.sina.com.cn/s/blog_7bd0d6a70101dixc.html
A simple Java Engineering implementation
1 First build a Java project in which a Web services Endpoint is built, with the following code:
PackageCom.WebService.Service;ImportJavax.jws.WebMethod;ImportJavax.jws.WebService;Importjavax.xml.ws.Endpoint; @WebService Public classHello {@WebMethod Publicstring Greeting (String str) {return"Hello" +str; } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub Try{Hello Hello=NewHello (); @SuppressWarnings ("Unused") Endpoint Endpoint= Endpoint.publish ("Http://localhost:7890/hello", hello); System.out.print ("Access Service has started ..."); } Catch(Exception ex) {System.out.println ("Access Service startup error:" +ex.getmessage (). toString ()); } }}
Compile and run the Java file, enter Http://localhost:7890/hello in the browser, you can see such as:
Click Hyperlink: Http://localhost:7890/hello? WSDL, as shown in the results:
2 Generating a client using Wsimport
Use the following: Wsimport-s-keep Http://localhost:7890/hello? Wsdl
Command parameter Description:
-D: Generate Directory of class files for client execution classes
-S: Generate a directory of source files for the client execution class
-P: Define the package name of the generated class
The results are as follows:
3 Writing the client program
Create a new Java project, copy the Java files in the project to the corresponding directory, build the client program, the code is as follows:
Packagecom. Webclientext.main;ImportCom.webservice.service.Hello;ImportCom.webservice.service.HelloService; Public classWebclientext { Public Static voidMain (String args[]) {HelloService Hello=NewHelloService (); Hello Helloproxy=Hello.gethelloport (); String Returnstr= Helloproxy.greeting ("Amy")); System.out.println ("Return data is:" +returnstr); }}
Run the program with the following results:
Java Development WebService Example (1)