WebService getting started instance tutorial, webservice instance tutorial

Source: Internet
Author: User

WebService getting started instance tutorial, webservice instance tutorial

What is WebService?

 

By using WebService, your application can publish information to the world or provide a function. It is a Web-based service that can be published, searched, and used through the Web.

The WebService script platform must support XML + HTTP.

HTTP is the most common Internet protocol.

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

 

Why WebService?

 

The most important thing is collaboration.

1. cross-platform calling (WebService is not limited to the operating system. You can call the WebService on linux on Windows, and vice versa. The same applies to other systems );

2. Cross-language calling (WebService is not limited to programming languages. You can call the WebService provided by C # in Java, and vice versa. The same applies to other languages );

3. Remote Call (by using WebService, your application can publish information to the world or provide a function, as long as there is an Internet ).

 

Use JDK to develop WebService

 

1. WebService server development

1. Create a server project. The project can be a Java Web project or a common Java Project, for example:

2. in the src package under the source code directory, create a java class to provide the WebService Service. Here I create a Function class through javax. jws. webService annotation provides services through javax. xml. ws. the publish method of the Endpoint class to publish the service and provide the publishing address and service class. The Code is as follows:

1 package com. jspweb. ws; 2 3 import javax. jws. webService; 4 import javax. xml. ws. endpoint; 5 6/** 7 * WebService server case 8 * @ author AlanLee 9 * @ version 2017/06/0210 * use @ WebService annotation to implement class 11 */12 @ WebService13 public class Function {14 public String transWords (String words) {15 String res = ""; 16 for (char ch: words. toCharArray () {17 res + = ch + ","; 18} 19 return res; 20} 21 22 public static void main (String [] args) {23 // use the Endpoint (terminal) class to publish webservice24 Endpoint. publish ("http: // localhost: 8089/service/function", new Function (); 25 System. out. println ("Publish Success"); 26} 27}

3. Start the main method to run this class and print the result Publish Success, indicating that the service has been published successfully:

4. Open your browser and visit our released address. You can see the following results:

 

5. Click the connection address next to the WSDL to open the WSDL configuration. The access result is as follows:

WSDL is an XML-based language used to describe Web Services and how to access Web Services.

 

2. WebService client development

1. Create a client project. The project can be a Java Web project or a common Java Project, for example:

2. Use the eclipse tool to generate the client code and follow the steps shown in the figure below:

① Right-click the selected project and choose 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 and click next:

④ Click the Finish button. eclipse will automatically generate the Code required for the WebService client under the src directory:

⑤: The generated code is as follows:

2. Compile the client test class code. I wrote a test class under the same level directory and run its main method. The result is as follows:

 

This simple WebService entry instance is complete.

 

Conclusion: Bodhi has no trees. There is nothing to do, where to make the dust!

 

Alimail: AlanLee

Blog: http://www.cnblogs.com/AlanLee

This article is from the blog site. You are welcome to join the blog site.

 

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.