Вы находитесь на странице: 1из 20

Create stand-alone web services applications with

Eclipse and Java SE 6, Part 1: The web service server


application
Fiona Lam
Software Engineer
IBM

13 November 2008

John Robertson
Staff Software Engineer
IBM
Use the Eclipse Integrated Development Environment (IDE) and Java Platform, Standard
Edition (Java SE) 6 to create a stand-alone web services application that can be run from
the console. In this tutorial, the first in a series, start by getting familiar with the Eclipse IDE.
Configure the environment; create projects, packages, and classes; then run the application
from the command line.
View more content in this series

Before you start


About this series
This tutorial series demonstrates how to create a stand-alone web services server and client
application that you can easily run from the command line with Java SE 6 rather than from within
Web application server containers. Using a simple Hello World example, you'll leverage the
Eclipse IDE, Java SE 6, and Apache Ant to easily create fully functioning web services server
and client applications. You'll also use the TCP/IP Monitor to examine the communication traffic
between the server and client, and use the Eclipse Web Services Explorer tool to test the web
service.

About this tutorial


This tutorial, Part 1 of the series, introduces you to publishing a web service application using the
Eclipse IDE, Java SE 6, and Ant. It lays the groundwork for Part 2, which describes the creation of
the web services client application.
Copyright IBM Corporation 2008
Create stand-alone web services applications with Eclipse and
Java SE 6, Part 1: The web service server application

Trademarks
Page 1 of 20

developerWorks

ibm.com/developerWorks/

Objectives
After completing this tutorial you should know:
What a web service is and the standards it uses in relation to a browser being used from
within Eclipse to view the published Web Services Description Language (WSDL) file.
How to create the server side of a web service, including how to install and configure the
Eclipse IDE and the Java Development Kit (JDK) so that they perform together to generate
the code that's compiled using Java SE 6.
How to use the Ant Java-based build tool within the Eclipse IDE to run a special Java
command to generate some of the code.

Prerequisites
This tutorial includes simple steps written for beginning- to intermediate-level Java programmers
with some working knowledge of the Java language and Ant builds. Novice to more advanced
Java developers will gain some knowledge of how to build, deploy, and run stand-alone web
services servers and distributed clients to provide firewall-friendly remote communications and
applications processing.

System requirements
To follow the examples, you need to download:
Eclipse IDE for Java Developers
Java SE 6
You don't have to download Ant, as its functionality is bundled with Eclipse. This tutorial uses the
Ganymede Package for the Eclipse IDE for Java EE Developers.

Set up your development environment


Install Java SE 6
1. Download and install the latest Java SE 6 JDK. Java SE 6 has many new features, including
web services APIs.
2. Double-click the executable file and follow the installation instructions. We recommend you
perform the typical installation and maintain all default settings, such as location.
3. When asked, select whether you want to install the Java runtime environment as the system
JVM and whether you want any browsers to be associated with the Java plug-in.
4. Click Finish to install.
5. Close any browser windows that are open.
6. When complete, you should be presented with a thank-you message confirming successful
installation. Click OK to close.
Note: Installing the Java Runtime Environment (JRE) as the system Java Virtual Machine (JVM)
means that it replaces any JVM found in the Microsoft Windows directory or places a new copy
Create stand-alone web services applications with Eclipse and
Java SE 6, Part 1: The web service server application

Page 2 of 20

ibm.com/developerWorks/

developerWorks

in there if one is not found. Associating any browsers with the Java plug-in means that this new
version of Java will be used for applets.

Install Eclipse
Eclipse is an open source, extensible development platform, which can be installed on almost all
operating systems. (Learn more about Eclipse.)
Installing Eclipse is fairly straightforward because there's no installation process:
Download the Eclipse IDE for Java EE Developers.
Extract the file to the desired location on your computer. You should then see a folder named
eclipse. It's a good idea to create a shortcut to the eclipse.exe file on your desktop for
convenience.

Configure Eclipse
When you first run Eclipse, the Welcome page is displayed, as shown in Figure 1. If you don't
want to read the Overview and other offerings, simply close that page and come back to it later by
selecting Help > Welcome.

Figure 1. Welcome screen

Configure Eclipse to use the Java SE 6 JDK you installed earlier; you want to associate your
project with this version of Java:
1. Select Window > Preferences > Java > Installed JREs, and click the Add button.
2. Enter a name, such as Java SE 6, to easily identify what version it is.
3. Click the Browse button and locate the directory where JRE 60 was installed.
4. Click OK (see Figure 2).

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 3 of 20

developerWorks

ibm.com/developerWorks/

Figure 2. Adding a new JRE

the list of installed JREs, as shown in Figure 3.


5. Select the Java SE 6 check box, then click OK.

The new JRE should now appear in

Figure 3. Selecting the new JRE

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 4 of 20

ibm.com/developerWorks/

developerWorks

6. To set compliance to the installed version of Java, select Window > Preferences > Java >
Compiler.
7. Select 1.6 from the Compiler compliance level drop-down list, as shown in Figure 4.

Figure 4. Setting compliance

Create a project
Next you create a project to construct your web services server. A project contains the source
code and other related files, and it lets you use the project as the source container or to set up
folders inside the project to organize files.
1. Select File > New > Project.
2. Expand the Java folder and click Java Project (see Figure 5).

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 5 of 20

developerWorks

ibm.com/developerWorks/

Figure 5. Creating a project in Eclipse

3. Click Next.
4. Enter a project name, such as wsServerExample, when prompted, as shown in Figure 6.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 6 of 20

ibm.com/developerWorks/

developerWorks

Figure 6. Entering project details in Eclipse

5. Select the Use default JRE radio button if it was previously selected by default; otherwise
select the Use a project specific JRE radio button, ensuring that it's Java SE 6.
6. Click Finish to associate your project with the Java JDK you installed earlier.
7. If you're prompted to switch Java perspective, click Yes.

Create the server


First you need to create a Java package to house your Java classes:
1. Select File > New > Package.
2. When the New Java Package window opens, enter a name for the package, such as
com.myfirst.wsServer, as shown in Figure 7.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 7 of 20

developerWorks

ibm.com/developerWorks/

Figure 7. Creating a package

Next you need to create a class for the server code:


3. Right-click the package name you just created, then select New > Class. Configure it as
shown in Figure 8.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 8 of 20

ibm.com/developerWorks/

developerWorks

Figure 8. Creating a class

4. Create your class as public with no main method stub.


Now that you've provided your package with a class, you can write the code for the server, as
shown in Listing 1.

Listing 1. Server code


package com.myfirst.wsServer;
import javax.jws.WebService;
@WebService
public class SayHello {
private static final String SALUTATION = "Hello";
public String getGreeting( String name ) {
return SALUTATION + " " + name;
}
}

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 9 of 20

developerWorks

ibm.com/developerWorks/

Note the text in bold in Listing 1. This is called an annotation, or metadata, which is used by the
Web Services Metadata Specification introduced in Java SE 5. Developers define their classes
and methods before applying annotations to them to indicate to the runtime engine how to enable
the class and its methods as a web service and web service operations. Java SE 6 comes bundled
with such an engine.
The @WebService annotation marks the SayHello class as implementing a web service, which
results in a deployable web service being produced. This particular annotation is a WSDL mapping
annotation and associates the Java source code to the WSDL elements that represent the web
service. (See Resources for more information about other annotations in Java SE 6.)

Generate the server code with Ant


After you've written the server application, you need to generate the web service-supporting code.
First, create a new Ant file called build.xml:
1. Right-click the project and select New > File.
2. Enter the name build.xml when prompted, then click Finish (see Figure 9).
3. Make sure this file opens with the Ant Editor by right-clicking it and selecting Open With >
Ant Editor. From now on, whenever you double-click this file, it opens with the Ant Editor.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 10 of 20

ibm.com/developerWorks/

developerWorks

Figure 9. Creating an Ant file

4. Enter the Ant project shown in Listing 2.

Listing 2. Ant script


<project default="wsgen">
<target name="wsgen" >
<exec executable="wsgen">
<arg line="-cp ./bin -keep -s ./src -d ./bin com.myfirst.wsServer.SayHello"/>
</exec>
</target>
</project>

5. To run the Ant build.xml file, right-click Run As and select Ant Build, which executes the Ant
file.
6. Make sure that this results in a BUILD SUCCESSFUL message in the Eclipse Console window, as
shown in Figure 10.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 11 of 20

developerWorks

ibm.com/developerWorks/

Figure 10. Ant build success

7. Return to the Eclipse project and refresh the project by right-clicking wsServerExample and
selecting Refresh. You should now see the generated code to run the web service created
under the new package called com.myfirst.wsServer.jaxws (see Figure 11).

Figure 11. Generated code

Publish the web service


After you've generated the code for the web service's server, you need to publish it so you can
start using it:
1. Create a new class under the com.myfirst.wsServer package you created, and call it
something like RunService.
2. Right-click the package and select New > Class, but this time select the option to create the
main method stub.
3. Write the code to publish your web service, as shown in Listing 3.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 12 of 20

ibm.com/developerWorks/

developerWorks

Listing 3. Publishing code


package com.myfirst.wsServer;
import Javax.xml.ws.Endpoint;
public class RunService {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("SayHello Web Service started.");
Endpoint.publish("http://localhost:8080/wsServerExample", new SayHello());
}
}

Java SE 6 provides new support for publishing web services. The Endpoint API simply
publishes the web service endpoint, which generates the WSDL at run time at a URL.
4. Run this class by right-clicking it and selecting Run As > Java Application. The Eclipse IDE
Console window should display. If it doesn't, select Window > Show View > Console. You
should see an indication that the web server has started, as shown in Figure 12.

Figure 12. Console with the service running

View the WSDL


Now that the server is up and running, you should test it to make sure it's working as expected:
1. Open the internal Web browser in Eclipse by selecting Window > Show View > Other >
General > Internal Web Browser.
2. type the URL, such as http://localhost:8080/wsServerExample?wsdl, which should display
the web service's WSDL text, as shown in Figure 13.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 13 of 20

developerWorks

ibm.com/developerWorks/

Figure 13. Console with the internal Web browser

3. When you're finished, you can stop the web service by clicking the red square in the Eclipse
Console view. However, to continue the tutorial the web service needs to remain running.

Test the server


Next you use the Eclipse Web Services Explorer tool to invoke the operations of a web service via
native WSDL and SOAP to test the getGreeting method of the web service you just created.
1. You may need to change to the Java EE perspective. Click Window > Open Perspective >
Other.
2. When the window appears, select Java EE.
3. Select Run > Launch the Web Services Explorer. Maximize the view by double-clicking its
tab. You should see the screen shown in Figure 14.

Figure 14. The Web Services Explorer

4. Click the icon indicated by the red circle. This displays the WSDL page, as shown in Figure
15.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 14 of 20

ibm.com/developerWorks/

developerWorks

Figure 15. WSDL page

5. In the Navigator pane, click WSDL Main. The Actions pane is updated, as shown in Figure
16.
6. Enter the WSDL URL, in this case http://localhost:8080/wsServerExample?wsdl, then click
the Go button.

Figure 16. Entering WSDL URL

7. The WSDL should successfully open, and you should see a screen similar to Figure 17.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 15 of 20

developerWorks

ibm.com/developerWorks/

Figure 17. Successfully opened WSDL

8. Next you invoke an operation by clicking getGreeting under Operations (shown in Figure 17).
This results in a screen similar to Figure 18.

Figure 18. Invoking an operation

9. Under getGreeting in the Body section, click the Add link (as shown in Figure 18) to add a
new row to the values table.
10. Enter a name (here, Fiona), and click the Go button.
Create stand-alone web services applications with Eclipse and
Java SE 6, Part 1: The web service server application

Page 16 of 20

ibm.com/developerWorks/

developerWorks

11. In the Status section, getGreetingResponse displays the result. You should see a You should
see a result like return (string): Hello Fiona (see Figure 19) in the Status section. You
might need to scroll or drag the views to see the result.

Figure 19. Result of the operation

Summary
Creating, generating, and publishing a web service server is as simple as using Eclipse and, of
course, Java SE 6. Stay tuned for Part 2 of this tutorial series where you'll build the stand-alone
client to use with this stand-alone web service server.

Appendix: A brief overview of web services terms and acronyms


Web service
According to W3C, a web service is the "software system designed to support interoperable
Machine to Machine interaction over a network." In other words, web services are programmatic
interfaces used for application-to-application communication. Typically, they are used as Web
applications that enable the communication between computers over a network, such as the
Internet.
Clients and servers communicate using XML messages that follow the SOAP standard. That is,
web services use XML to code and decode data and SOAP to transport it using open protocols.
Two of the basic elements of web services platforms are SOAP and WSDL.

XML
Extensible Markup Language (XML) lets users define their own elements. It's a general purpose
specification facilitating the sharing of structured data across different information systems,
Create stand-alone web services applications with Eclipse and
Java SE 6, Part 1: The web service server application

Page 17 of 20

developerWorks

ibm.com/developerWorks/

typically across a network. XML is designed to carry information and not to display it. In other
words, XML does not actually do anything other than to structure, store, and transport information;
it's just plain text.

SOAP
SOAP used to stand for Simple Object Access Protocol, but this was dropped in version 1.2
because it was believed to be too misleading. It's a lightweight communication protocol that lets
applications exchange information over networks using XML, or more simply, for accessing a web
service. SOAP allows applications to communicate with each other regardless of which operating
system they're running on and what programming language they were written in.

WSDL
A WSDL is an application-readable Web Services Description Language. It's used to describe the
web service's features and how it should be called by the client application. That is, it describes all
the methods and its signatures, the namespaces, plus the handling Uniform Resource Identifier
(URI) for the web service in an XML document. The URI names a resource on a network.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 18 of 20

ibm.com/developerWorks/

developerWorks

Resources
Learn
Start here for more Eclipse IDE project resources.
Visit the W3C site.
In the Architecture area on developerWorks, get the resources you need to advance your
skills in the architecture arena.
Discover the new Java SE 6 features.
The SOA and web services zone on IBM developerWorks hosts hundreds of informative
articles and introductory, intermediate, and advanced tutorials on how to develop web
services applications.
The IBM SOA Web site offers an overview of SOA and how IBM can help you get there.
Check out a quick web services on demand demo.
Get products and technologies
Download the Eclipse IDE for Java EE developers.
Download Java SE 6.
Download IBM product evaluation versions and get your hands on application development
tools and middleware products from DB2, Lotus, Rational, Tivoli, and WebSphere.
Discuss
Participate in the discussion forum for this content.
Get involved in the developerWorks community by participating in developerWorks blogs.

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 19 of 20

developerWorks

ibm.com/developerWorks/

About the authors


Fiona Lam
Fiona Lam is a software engineer and works for the Australian Development
Laboratory as part of IBM Tivoli Security Development.

John Robertson
John Robertson is a staff software engineer and also works for the Australian
Development Laboratory as part of IBM Tivoli Security Development.
Copyright IBM Corporation 2008
(www.ibm.com/legal/copytrade.shtml)
Trademarks
(www.ibm.com/developerworks/ibm/trademarks/)

Create stand-alone web services applications with Eclipse and


Java SE 6, Part 1: The web service server application

Page 20 of 20

Вам также может понравиться