Java WebService simple instance, javawebservice

Source: Internet
Author: User

Java WebService simple instance, javawebservice

Before starting the following tutorial, let's take a look at the things you should pay attention to in order to avoid unnecessary repeated operations.

 

I. Preparations (the following are the tools used for this instance)

1. MyEclipse10.7.1

2. JDK 1.6.0 _ 22

 

2. Create a server

1. Create a Web Service Project named TheService ].



 

 

 

2. Create a [Class] Class named [ServiceHello], which is located under the [com. hyan. service] package.



 

 

 

3. Compile the method code called by the client.



 

 

4. Compile

Note:If compilation fails, set the jdk referenced by this project to version 1.6.0 _ 17 or later.



 

 

 

5. Test Results

Test address:Http: // localhost: 9001/Service/ServiceHello? Wsdl



 

 

3. Generate a client

1. Create a Web Service Project named TheClient ].



 

 

 

 

2. Run the generate command in the Command Prompt window.

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

Example:

Wsimport-s G :\\ workspace \ webService \ TheClient \ src-p com. hyan. client-keep http: // localhost: 9001/Service/ServiceHello? Wsdl

Note:

1) The "src directory" address cannot contain spaces

2) do not miss "? Wsdl"



 

 

3. Refresh the project and check the generated class.



 

 
 

 

Iv. Final Test

1. Create a [Class] Class named [ServiceTest], which is located in the [com. hyan. test] package.







 

 

2. Compile the test method, call the server method, and print the returned results.



 

 

 

5. Notes

1. JDK version is too low

An exception similar to the following is reported:Runtime modeler error: Wrapper class com. hyan. service. jaxws. GetValue is not found. Have you run APT to generate them?



 

Cause:JDK version earlier than 1.6.0 _ 17

Solution:Adjust the JDK version referenced by this server project to a later JDK version.



 

 

 

 

 

 

 

 

 

 

 

 

2. Generate command paths with spaces

An exception similar to the following is reported:Directory not found: G: \ SVN_FILE \ GB \ 05



 

Cause:The client src path cannot contain spaces

Solution:Create a new client in a folder without spaces.

 

3. Incomplete command generation

An exception similar to the following is reported:[ERROR] failed. noservice = cocould not find wsdl: service in the provided WSDL (s ):



 

Cause:"?" Is missing at the end of the generated command. Wsdl"

Solution:Complete the configuration.

Command example with incomplete errors:

Wsimport-s G :\\ workspace \ webService \ TheClient \ src-p com. hyan. client-keep http: // localhost: 9001/Service/ServiceHello

Correct and complete command example:

Wsimport-s G :\\ workspace \ webService \ TheClient \ src-p com. hyan. client-keep http: // localhost: 9001/Service/ServiceHello? Wsdl

 

Vi. sample source code

Download Server Source Code

Download client source code

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.