WebService Introductory Example Tutorial

Source: Internet
Author: User
Tags webservice annotation wsdl

What is WebService

By using WebService, your application can publish information to the world, or provide a feature that is web-based and published, found, and used through the web.

The WebService scripting platform needs to support xml+http.

The HTTP protocol is the most commonly used Internet protocol.

XML provides a language that can be used between different platforms and programming languages.

Why to use WebService

The most important thing is to work together.

1. Cross-platform calls (WebService is not limited to the operating system, you can increase the WebService service on Linux on Windows, and vice versa);

2. Cross-language calls (WebService is not limited to programming languages, you can invoke the WebService service provided by the C # language in the Java language, and vice versa);

3. Can be called remotely (by using WebService, your application can publish information to the world, or provide a feature, as long as the Internet is available).

Using the JDK to develop webservice

I. WebService server Development

1. Create a server-side project that can be either a Java Web project or an ordinary Java project, such as:

2. Create a new Java class in the package below the source code catalog SRC to provide the WebService service, I have created a function class here, Provide services through Javax.jws.WebService annotations, publish the service through the publish method of the Javax.xml.ws.Endpoint class, and provide the published address and the published service class, with the following code:

1  Packagecom.jspweb.ws;2 3 ImportJavax.jws.WebService;4 ImportJavax.xml.ws.Endpoint;5 6 /**7 * WebService Service-side case8  * @authorAlanlee9  * @version2017/06/02Ten * Implement class using @webservice annotation annotation One  */ A @WebService -  Public classFunction { -      Publicstring Transwords (string words) { theString res = ""; -          for(CharCh:words.toCharArray ()) { -Res + = ch+ ","; -         } +         returnRes; -     } +      A      Public Static voidMain (string[] args) { at         //use Endpoint (Terminal) class to publish WebService -Endpoint.publish ("Http://localhost:8089/service/function",NewFunction ()); -System.out.println ("Publish Success"); -     } -}

3. Start the Main method to run this class and print out the result publish Success, indicating that the service was published successfully:

4. Open the browser, visit our published address, you can see the effect:

5. Click the connection address at the back of the WSDL to open the WSDL configuration and access the results as follows:

WSDL is an XML-based language that describes Web services and how to access Web services.

Second, WebService client development

1. Create a client project that can be a Java Web project or a generic Java project, such as:

2. Use the Eclipse tool to generate the client code and follow the diagram step-by-step:

① Select item Click right mouse button New>other ... :

② Enter the web in the Wizards box, select the Web Service Client, and click the Next button:

③ Enter the access address of the WSDL service description file, click Next:

④ Click the Finish button, eclipse will automatically help us generate the code required for the WebService client in the SRC directory:

⑤: The generated code is as follows:

2. Write the client test class code, I write a test class under the sibling directory, and run its main method, the results are as follows:

Such a simple webservice Getting started instance is complete.

Conclusion: Bodhi is not a tree, mirror also not Taiwan. There is nothing, where to provoke dust!

Cute bo master: Alanlee

Blog Address: Http://www.cnblogs.com/AlanLee

This article is from the blog garden, welcome everyone to join the blog park.

WebService Introductory Example Tutorial

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.