Академический Документы
Профессиональный Документы
Культура Документы
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
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.
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.
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).
Page 3 of 20
developerWorks
ibm.com/developerWorks/
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.
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).
Page 5 of 20
developerWorks
ibm.com/developerWorks/
3. Click Next.
4. Enter a project name, such as wsServerExample, when prompted, as shown in Figure 6.
Page 6 of 20
ibm.com/developerWorks/
developerWorks
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.
Page 7 of 20
developerWorks
ibm.com/developerWorks/
Page 8 of 20
ibm.com/developerWorks/
developerWorks
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.)
Page 10 of 20
ibm.com/developerWorks/
developerWorks
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.
Page 11 of 20
developerWorks
ibm.com/developerWorks/
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).
Page 12 of 20
ibm.com/developerWorks/
developerWorks
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.
Page 13 of 20
developerWorks
ibm.com/developerWorks/
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.
4. Click the icon indicated by the red circle. This displays the WSDL page, as shown in Figure
15.
Page 14 of 20
ibm.com/developerWorks/
developerWorks
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.
7. The WSDL should successfully open, and you should see a screen similar to Figure 17.
Page 15 of 20
developerWorks
ibm.com/developerWorks/
8. Next you invoke an operation by clicking getGreeting under Operations (shown in Figure 17).
This results in a screen similar to Figure 18.
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.
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.
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.
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.
Page 19 of 20
developerWorks
ibm.com/developerWorks/
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/)
Page 20 of 20