Service side:
PackageCom.yinfu.service;ImportJavax.jws.WebService;Importjavax.xml.ws.Endpoint; @WebService Public classTestwebsservice { Publicstring SayHello (string username) {return"Hello:" +username; } Public Static voidMain (string[] args) {endpoint.publish ("Http://localhost:8075/com.yinfu.service.TestWebsService",NewTestwebsservice ()); System.out.println ("Success"); }}
In the class that you want to publish as WebService, add annotation @webservice, the method of this class becomes WebService method, then through endpoint publish method, publish this service, to this, one of the simplest WebService fix. Run the Main method and enter "HTTP://LOCALHOST:8075/COM.YINFU.SERVICE.TESTWEBSSERVICE?WSDL" in the browser to See your WSDL information.
OK, explain the service side no problem, build success
Client:
At the command line, enter the command WSIMPORT-P [package name]-keep [published service address? WSDL] to generate client code, such as client code WSIMPORT-P COM.YINFU.SERVICE.CLIENT-KEEP/HTTP//For the raw cost example Localhost:8075/com.yinfu.service.testwebsservice?wsdl ", of course, if you already have a Java environment variable in place. The console will display
Note Now that you need to find the generated client Java code, the console points to where the generated code is, for example, my console points to C:\Users\Administrator\ so I generated the Java file in C:\Users\Administrator\ Com\yinfu\service\client\, this place to pay attention to, there are many people do not know where the generated code to go!
OK, copy the generated code to the client's project,
Packagecom.yinfu.service.client; Public classhelloclient {/** * @paramargs*/ Public Static voidMain (string[] args) {testwebsserviceservice MyService=NewTestwebsserviceservice (); Testwebsservice Ms=Myservice.gettestwebsserviceport (); String s= Ms.sayhello ("Why"); System.out.println (s); }}
With these generated client code, you can invoke this WebService service: Execute code, output: Hello why. When running, pay attention to the service side of the project service is started.