Using JAX-ws to build WebServices server and client in eclipse

Source: Internet
Author: User
Tags wsdl

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.