Java Development WebService Example (1)

Source: Internet
Author: User
Tags wsdl

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)

Related Article

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.