Develop JAX-WS Web Services on myeclipse6.5

Source: Internet
Author: User
Tags server hosting

Table of contents

1. Introduction

This document will outline the process of developing a JAX-WS web service and deploying it using myeclipse 6.5 to the internal myeclipse Tomcat server. the Web Service used in this tutorial will be a very simple calculator service that provides add, subtract, multiply and divide operations to the caller.

Myeclipse also supports developing web services using the existing xfire framework from previous myeclipse releases. for folks needing to develop and deploy WebSphere JAX-RPC or WebSphere JAX-WS Web Services, please take a look at our myeclipse blue edition of The myeclipse IDE.

Additional resources covering Web Service creation using JAX-RPC, JAX-WS or xfire are stored in the of this document.

2. system requirements this tutorial was created with myeclipse 6.5. If you are using another version of myeclipse (possibly newer), most of these screens and instructions showould still be very similar.

If you are using a newer version of myeclipse and notice portions of this tutorial looking different than the screens you are seeing, please and we will make sure to resolve any inconsistencies.

3. Creating a project to get started we will create a simple Web Service Project by selecting web service project from the new toolbar menu:

Note: A JAX-WS web service can also be generated in any existing Java ee 5 web project.

Name the project webserviceproject. note that JAX-WS support is only available for Java ee 5 projects and later, if you need to use an older project type (J2EE 1.4 or 1.3) You must use the xfire Web Services Support instead:

 

Now that we have created the web project, we can now create the simple Java class that we will use as the basis of our web service.

4. creating the service class is nothing more than a plain Java class that will provide implementations for the methods we want to expose as web service. in this special tutorial we are going to write a simple 19-line calculator class that implements a few typical operations found on a calculator.

Let's first create a package for our new class under our source directory by right-clicking on the source directory and selecting new> package:

The package name we are going to use for this tutorial is com. myeclipseide. WS:

Now we are going to create a new class, calculator, in the new package we just created:

As mentioned above, this simple class is a calculator implementation that provides the functions:

Add
Subtract
Multiply
Divide
For any 2 ints. The implementation of the class looks like:

Package com. myeclipseide. ws;

Public class calculator {
Public int add (int A, int B ){
Return (A + B );
}

Public int subtract (int A, int B ){
Return (a-B );
}

Public int multiply (int A, int B ){
Return (A * B );
}

Public int divide (int A, int B ){
Return (A/B );
}
}

Calculator class implementation

As you can see from the source code, this class is a very simple pojo offering 4 operations. There is no use of special annotations, interfaces or base classes.

5. creating a Web Service now that we have our service class written (calculator. java) We need to create a web service that exposes that server class as a web service. to do that we start by clicking the new Web Service toolbar button:

On the next screen in the Strategy Section, select the bottom-up scenario since we have our calculator class already and want to generate a JAX-WS web service from it:

This is the last screen of the web service wizard. on this screen you have to select the Java Bean that implements the operations for your web service, in our case it will be the single calculator class that we wrote in the previous section:

Once you set the Java Bean class, myeclipse will automatically fill out the remainder of the wizard fields for you. of course you are welcome to customize or adjust them if you want, but in this case we are going to leave them all the way they are. select generate WSDL in project and hit finish.

After clicking finish myeclipse will generate the JAX-WS delegate class as well as the necessary JAX-WS descriptor into the WEB-INF directory of your web project. myeclipse will also modify the web. XML file to include the new web service mappings so you can deploy and use the web service.

Checking your project contents will show you all the artifacts that have been generated for you in order for that web service to be deployable to the target server:

Now that our Web Service is created, we are ready to deploy it and test it out.

6. deploying & testing the Web Service JAX-WS is part of the Java ee 5 specification, if you are deploying your project to a full Java ee 5-compliant server, you shoshould be ready to deploy your project right now without any further changes to the libraries. however, there are some common servers, like jetty or tomcat, that do not implement the entire Java ee 5 spec and will need the JAX-WS libraries deployed with your project in order to run.

Important: In myeclipse we have tried to make this process easier by including the JAX-WS RI (Metro 1.1) with our embedded myeclipse Tomcat server, so your web services will run out of the box on myeclipse tomcat, But if you plan to deploy to an external server that does not provide the JAX-WS libraries, you will need to follow the steps below on How to augment your build path to include them (so they are deployed ). you can also install the JAX-WS libraries directly into your application server's/lib directory ususally if you don't want to add the libraries directly to your project; please check your application server's documentation for more information regarding that.

If you do want to add the libraries to your project build path, please keep reading section 6.1, otherwise skip to section 6.2.

6.1 adding JAX-WS libraries to your build path

First thing we need to do is open our project's Java build path> libraries preference page by right-clicking on our project, and going to properties, from there we want to click the libraries button:

Now you want to click myeclipse libraries and hit next,

Development of WebService Based on JAX-WS --- 22008-09-15

Then scroll down and select both the JAX-WS library containers to be added to your project's build path:

So we end up wth something like the following:

 

Now we have the JAX-WS libraries in our project's build path, and they will be deployed along with our project to myeclipse tomcat.

6.2 deploying and running the JAX-WS Web Service

The fastest way to deploy our Web Service is to deploy our web project using the run as or debug as action of myeclipse server application. we can do that by right-clicking on our project, going down to debug as (or run as) and selecting myeclipse server application:

If you have multiple server connectors configured, myeclipse will ask you which one you want to use, for the purpose of this tutorial select myeclipse tomcat. if you don't have any connectors configured, myeclipse Tomcat will be used automatically for you to deploy your project to and then run.

Now myeclipse will perform the following steps for you automatically:

Package our web project, and deploy it in exploded format to the Application Server
Start the application server for us, loading our web project
The myeclipse Web browser will popup and show you the default index. JSP page of our web app, we don't actually need this because we aren't testing a Web page, so you can close this view.

6.3 testing our JAX-WS Web Service

Now we are ready to connect to the web service and test it out. The first thing we need to do is load the Web Services explorer from the toolbar by clicking it's button:

 

After the Web Services explorer is loaded, we want to click on the WSDL mode button, then click on WSDL main to open the open WSDL screen. then we want to enter the URL for our Web Service WSDL which is:

Http: // localhost: 8080/webserviceproject/calculatorport? WSDL

 

We can break the URL down into the following components to understand how we arrived at that URL:

Http: // localhost: 8080 = we know the server is running on localhost, and we know the default Tomcat port is 8080.
/Webserviceproject = we know by default the web context-root that is used to deploy web projects matches the name of the projects. since we didn't mimize our web context-root for this project, it will be the same name as our project name.
/Calculatorport = as we saw from the last screenshot in, when our JAX-WS Web Service was generated, it was bound using a servlet-mapping in the web. xml file to the/calculatorport path.
? WSDL = This is a universal query string argument that can be added to the end of any web service which will tell the Web service to return it's full WSDL to the caller. in this case, the WSDL is returned to our web services explorer tool which loads it up, and displays the Web Services exposed operations to us.

Now the Web Services Explorer will load up all the operations exposed to us from this web service and display them to us:

 

For the purposes of testing this web service, We can click one of the operations to use the explorer to test them. Let's click the add operation.

Now we are shown the invoke a WSDL operation screen. we are shown the endpoint we are going to test (calculator) and each argument the operation takes along with a field to enter values for each operation.

Enter the values "10" and "20" to add, then click go:

 

After clicking go, down in the Status view you'll see the response from the Web service, which in this case was "30", and is correct.

Now that we tested our web service, and it works, let's close the Web Services explorer and move onto the next step... creating a Web service client!

7. creating a client for the Web Service now that we have deployed our web service and tested it, we will use myeclipse to generate a web service client for us. the Web service client will allow us to interact directly with the web service and all it's exposed operations without needing to write all the same alling or connection code ourselves.

The first step to create the Web service client will be to create a new Java project to put the code into. We will do that by clicking the new toolbar button, then clicking Java project:

 

Now give your project a name and click Finish:

 

After the project has been created, we now want to create a new Web service client. Select New web service client from the Web Service toolbar menu:

 

Now we select the project we want to generate the client into, and the type of client we want. in this case we are putting the client into the webserviceclientproject we created and want it to be a JAX-WS client:

 

The last step of the web service client creation is to specify either a wsdl file or a wsdl url for the Wizard to retrieve the Web Service WSDL from. in our case we are using the URL and generate the client into the new package COM. myeclipseide. WS. client:

Http: // localhost: 8080/webserviceproject/calculatorport? WSDL

 

Now myeclipse will load the WSDL for the Web service you are attempting to create a client for and validate it for you, leader you know of any problems that may exist with the WSDL:

 

NOTE: If any error occurs with validation, make sure the Web Service is deployed and the application server hosting it is running. if you are trying to generate a client to a 3rd party web service and get errors during the validation process, please bring it to the attention of the author of the web service if possible so it can be corrected.

Now you can click Finish to have the client generated for you. After the client has been generated for you,

ArticleSource: http://www.diybl.com/course/3_program/java/javajs/200896/139321_2.html

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.