20
© Copyright IBM Corporation 2008 Trademarks Create stand-alone web services applications with Eclipse and Java SE 6, Part 1: The web service server application Page 1 of 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 John Robertson Staff Software Engineer IBM 13 November 2008 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.

Ws Eclipse Javase1 PDF

Embed Size (px)

DESCRIPTION

About WebServices

Citation preview

Page 1: Ws Eclipse Javase1 PDF

© Copyright IBM Corporation 2008 TrademarksCreate stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 1 of 20

Create stand-alone web services applications withEclipse and Java SE 6, Part 1: The web service serverapplicationFiona LamSoftware EngineerIBM

John RobertsonStaff Software EngineerIBM

13 November 2008

Use the Eclipse Integrated Development Environment (IDE) and Java™ Platform, StandardEdition (Java SE) 6 to create a stand-alone web services application that can be run fromthe 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 applicationfrom 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 clientapplication that you can easily run from the command line with Java SE 6 rather than from withinWeb application server containers. Using a simple Hello World example, you'll leverage theEclipse IDE, Java SE 6, and Apache Ant to easily create fully functioning web services serverand client applications. You'll also use the TCP/IP Monitor to examine the communication trafficbetween the server and client, and use the Eclipse Web Services Explorer tool to test the webservice.

About this tutorial

This tutorial, Part 1 of the series, introduces you to publishing a web service application using theEclipse IDE, Java SE 6, and Ant. It lays the groundwork for Part 2, which describes the creation ofthe web services client application.

Page 2: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 2 of 20

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 fromwithin 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 theEclipse IDE and the Java Development Kit (JDK) so that they perform together to generatethe 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 Javacommand to generate some of the code.

Prerequisites

This tutorial includes simple steps written for beginning- to intermediate-level Java programmerswith some working knowledge of the Java language and Ant builds. Novice to more advancedJava developers will gain some knowledge of how to build, deploy, and run stand-alone webservices servers and distributed clients to provide firewall-friendly remote communications andapplications 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 theGanymede 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, includingweb services APIs.

2. Double-click the executable file and follow the installation instructions. We recommend youperform 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 systemJVM 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

Page 3: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 3 of 20

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

Install Eclipse

Eclipse is an open source, extensible development platform, which can be installed on almost alloperating 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 forconvenience.

Configure Eclipse

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

Figure 1. Welcome screen

Configure Eclipse to use the Java SE 6 JDK you installed earlier; you want to associate yourproject 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 4: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 4 of 20

Figure 2. Adding a new JRE

The new JRE should now appear inthe list of installed JREs, as shown in Figure 3.

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

Figure 3. Selecting the new JRE

Page 5: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 5 of 20

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 sourcecode and other related files, and it lets you use the project as the source container or to set upfolders inside the project to organize files.

1. Select File > New > Project.2. Expand the Java folder and click Java Project (see Figure 5).

Page 6: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 6 of 20

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.

Page 7: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 7 of 20

Figure 6. Entering project details in Eclipse

5. Select the Use default JRE radio button if it was previously selected by default; otherwiseselect 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.

Page 8: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 8 of 20

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 asshown in Figure 8.

Page 9: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 9 of 20

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, asshown 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; }}

Page 10: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 10 of 20

Note the text in bold in Listing 1. This is called an annotation, or metadata, which is used by theWeb Services Metadata Specification introduced in Java SE 5. Developers define their classesand methods before applying annotations to them to indicate to the runtime engine how to enablethe class and its methods as a web service and web service operations. Java SE 6 comes bundledwith such an engine.

The @WebService annotation marks the SayHello class as implementing a web service, whichresults in a deployable web service being produced. This particular annotation is a WSDL mappingannotation and associates the Java source code to the WSDL elements that represent the webservice. (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.

Page 11: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 11 of 20

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 Antfile.

6. Make sure that this results in a BUILD SUCCESSFUL message in the Eclipse Console window, asshown in Figure 10.

Page 12: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 12 of 20

Figure 10. Ant build success

7. Return to the Eclipse project and refresh the project by right-clicking wsServerExample andselecting Refresh. You should now see the generated code to run the web service createdunder 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 canstart using it:

1. Create a new class under the com.myfirst.wsServer package you created, and call itsomething like RunService.

2. Right-click the package and select New > Class, but this time select the option to create themain method stub.

3. Write the code to publish your web service, as shown in Listing 3.

Page 13: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 13 of 20

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 simplypublishes 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 IDEConsole window should display. If it doesn't, select Window > Show View > Console. Youshould 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 displaythe web service's WSDL text, as shown in Figure 13.

Page 14: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 14 of 20

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 EclipseConsole 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 vianative 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 Figure15.

Page 15: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 15 of 20

Figure 15. WSDL page

5. In the Navigator pane, click WSDL Main. The Actions pane is updated, as shown in Figure16.

6. Enter the WSDL URL, in this case http://localhost:8080/wsServerExample?wsdl, then clickthe Go button.

Figure 16. Entering WSDL URL

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

Page 16: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 16 of 20

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 anew row to the values table.

10. Enter a name (here, Fiona), and click the Go button.

Page 17: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 17 of 20

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

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

Appendix: A brief overview of web services terms and acronymsWeb serviceAccording to W3C, a web service is the "software system designed to support interoperableMachine to Machine interaction over a network." In other words, web services are programmaticinterfaces used for application-to-application communication. Typically, they are used as Webapplications that enable the communication between computers over a network, such as theInternet.

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.

XMLExtensible Markup Language (XML) lets users define their own elements. It's a general purposespecification facilitating the sharing of structured data across different information systems,

Page 18: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 18 of 20

typically across a network. XML is designed to carry information and not to display it. In otherwords, 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.2because it was believed to be too misleading. It's a lightweight communication protocol that letsapplications exchange information over networks using XML, or more simply, for accessing a webservice. SOAP allows applications to communicate with each other regardless of which operatingsystem 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 theweb service's features and how it should be called by the client application. That is, it describes allthe 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 19: Ws Eclipse Javase1 PDF

ibm.com/developerWorks/ developerWorks®

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 19 of 20

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 webservices 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 20: Ws Eclipse Javase1 PDF

developerWorks® ibm.com/developerWorks/

Create stand-alone web services applications with Eclipse andJava SE 6, Part 1: The web service server application

Page 20 of 20

About the authors

Fiona Lam

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

John Robertson

John Robertson is a staff software engineer and also works for the AustralianDevelopment 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/)