WebService Application -- use java to develop WebService programs and javawebservice

Source: Internet
Author: User
Tags webservice annotation

WebService Application -- use java to develop WebService programs and javawebservice

Use Eclipse to develop the first WebService program. The Tool used in this example is Spring-Tool-Suite, which is essentially different from Eclipse. The Development Environment jdk1.7

1. Development steps:

1. Create a WebService Project

 

Create a HelloService class and place it in the com. test. webservice package.

Write the server program as follows:

1 package com. test. webservice; 2 import javax. jws. webService; 3 import javax. xml. ws. endpoint; 4 // Add the WebService annotation. Note that the package name should not be referenced incorrectly. 5 @ WebService 6 public class HelloService {7/*** this method is called by a client, return String 9 * @ param name10 * @ return11 * @ return String12 */13 public String getValue (String name) {14 return "Hello" + name; 15} 16/** 17 * main is used to publish the server to localhost. The second parameter is the created local object 18 * @ param args19 * @ return void20 */ 21 public static void main (String [] args) {22 Endpoint. publish ("http: // localhost: 1008/service/serviceHello", new HelloService (); 23 System. out. println ("published successfully! "); 25} 27}

Run and compile the class. The result is as follows:

Enter http: // localhost: 1008/service/serviceHello? in the browser? Wsdl. You can see the information, indicating that the compilation is successful!

2. Generate the client

2.1 to generate a client program, you can create another WebService project, or create a package based on the original project. For convenience, use the second method: Create a package named client.

2.2 Open the Command Prompt window and execute the command generation.

Format:Wsimport-s "src directory"-p "generate class package name"-keep "wsdl publishing address"

Example:

C: \ Users \ wangsj> wsimport-s E :\\ workingFoilder \ projectSpace \ webservice \ src-p com. test. webservice. client-keep http: // localhost: 1008/service/serviceHello? Wsdl

Note:

1) The "src directory" address cannot contain spaces

2) do not miss "? Wsdl"

 

If no error is reported, the compilation is successful! Refresh the project and you can see the class that generates the client response:

3. Create a test class for testing

1 package com. test. webservice. test; 2 // note that the package name cannot reference 3 import com. test. webservice. client. helloService; 4 import com. test. webservice. client. helloServiceService; 5 public class Test {6 7 public static void main (String [] args) {8 // initialize the object and obtain port 9 HelloService sh = new HelloServiceService (). getHelloServicePort (); 10 // call the server method 11 String name = sh. getValue ("WebService"); 12 System. out. println (name); 13} 14 15}

The running result shows that the first WebService program is successfully created!

Iv. Summary

1. if an exception occurs during running, for example, runtime modeler error: Wrapper class com. hyan. service. jaxws. GetValue is not found. Have you run APT to generate them ?, This indicates that the current JDK version is too low. We recommend that you upgrade it to version 1.7 and try again.

2. if the path is written incorrectly or contains spaces, an error will be reported. I wrote an error during writing, so be sure to take it seriously when writing the path.

3. Do not lose the wsdl at the end.

 

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.