0% found this document useful (0 votes)
77 views

Creating, Publishing, Testing and Describing A Web Service: Example

This document describes a web service that performs operations on large integers represented as strings. It outlines how to create the web service in NetBeans, define the HugeInteger class with JAX-WS annotations to indicate it is a web service, and define methods like add, subtract, bigger, smaller and equals that operate on the huge integers and are exposed as web methods. The methods take the huge integers as string parameters, perform the operations, and return strings or booleans.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Creating, Publishing, Testing and Describing A Web Service: Example

This document describes a web service that performs operations on large integers represented as strings. It outlines how to create the web service in NetBeans, define the HugeInteger class with JAX-WS annotations to indicate it is a web service, and define methods like add, subtract, bigger, smaller and equals that operate on the huge integers and are exposed as web methods. The methods take the huge integers as string parameters, perform the operations, and return strings or booleans.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

1

Creating, Publishing, Testing and


Describing a Web Service

 Example: HugeInteger web service


– Provides methods that take two “huge integers”
(represented as Strings)

– Can determine their sum, their difference, which is larger,


which is smaller or whether the two numbers are equal
2

Creating a Web Application Project and Adding


a Web Service Class in NetBeans

 In Netbeans, you focus on the logic of the web


service and let the IDE handle the web service’s
infrastructure

 To create a web service in NetBeans


– Create a project of type Web Application
– The IDE generates additional files that support the web
application
3

Defining the HugeInteger Web Service in


Netbeans

 Each new web service class created with the JAX-


WS APIs is a POJO (plain old Java object)
– You do not need to extend a class or implement an
interface to create a Web service

 When you compile a class that uses these JAX-


WS 2.0 annotations, the compiler creates the
compiled code framework that allows the web
service to wait for and respond to client requests
4

Defining the HugeInteger Web Service in


Netbeans

@WebService annotation

– Indicates that a class represents a web service

– Optional element name specifies the name of the proxy


class that will be generated for the client

– Optional element serviceName specifies the name of the


class that the client uses to obtain a proxy object.
5

 Netbeans places the @WebService annotation at the


beginning of each new web service class you create

 You can add the optional name and serviceName


elements in the annotation’s parentheses

 Methods that are tagged with the @WebMethod


annotation can be called remotely

 Methods that are not tagged with @WebMethod are not


accessible to clients that consume the web service
6

 @WebMethod annotation
– Optional operationName element to specify the method
name that is exposed to the web service’s client

 Parameters of web methods are annotated with


the @WebParam annotation
– Optional element name indicates the parameter name that
is exposed to the web service’s clients
1 // Fig. 28.2: HugeInteger.java 7
2 // HugeInteger web service that performs operations on large integers.
3 package com.deitel.jhtp7.ch28.hugeinteger;
4
5 import javax.jws.WebService; // program uses the annotation @WebService
6 import javax.jws.WebMethod; // program uses the annotation @WebMethod
7 import javax.jws.WebParam; // program uses the annotation @WebParam
8
Import the
9 @WebService( // annotates the class as a web service
annotations used
10 name = "HugeInteger", // sets class name
in this example
11 serviceName = "HugeIntegerService" ) // sets the service name
12 public class HugeInteger
13 {
14 private final static int MAXIMUM = 100; // maximum number of digits
15 public int[] number = new int[ MAXIMUM ]; // stores the huge integer
16
17 // returns a String representation of a HugeInteger
18 public String toString()
Indicate that class
19 { HugeInteger is a
20 String value = ""; web service
21
22 // convert HugeInteger to a String
23 for ( int digit : number )
24 value = digit + value; // places next digit at beginning of value
25
26 // locate position of first non-zero digit
27 int length = value.length();
28 int position = -1;
29
30 for ( int i = 0; i < length; i++ ) 8
31 {
32 if ( value.charAt( i ) != '0' )
33 {
34 position = i; // first non-zero digit
35 break;
36 }
37 } // end for
38
39 return ( position != -1 ? value.substring( position ) : "0" );
40 } // end method toString
41
42 // creates a HugeInteger from a String
43 public static HugeInteger parseHugeInteger( String s )
44 {
45 HugeInteger temp = new HugeInteger();
46 int size = s.length();
47
48 for ( int i = 0; i < size; i++ )
49 temp.number[ i ] = s.charAt( size - i - 1 ) - '0';
50
51 return temp;
52 } // end method parseHugeInteger
53
54 // WebMethod that adds huge integers represented by String arguments 9
55 @WebMethod( operationName = "add" ) Outline
56 public String add( @WebParam( name = "first" ) String first,
57 @WebParam( name = "second" ) String second ) Declare that method
58 { add is a web method
59 int carry = 0; // the value to be carried
60 HugeInteger operand1 = HugeInteger.parseHugeInteger( first );
61 HugeInteger operand2 = HugeInteger.parseHugeInteger( second );
62 HugeInteger result = new HugeInteger(); // stores addition result
63
64 // perform addition on each digit
65 for ( int i = 0; i < MAXIMUM; i++ )
66 {
67 // add corresponding digits in each number and the carried value;
68 // store result in the corresponding column of HugeInteger result
69 result.number[ i ] =
70 ( operand1.number[ i ] + operand2.number[ i ] + carry ) % 10;
71
72 // set carry for next column
73 carry =
74 ( operand1.number[ i ] + operand2.number[ i ] + carry ) / 10;
75 } // end for
76
77 return result.toString();
78 } // end WebMethod add
79
80 // WebMethod that subtracts integers represented by String arguments 10
81 @WebMethod( operationName = "subtract" ) Outline
82 public String subtract( @WebParam( name = "first" ) String first,
83 @WebParam( name = "second" ) String second )
84 {
85 HugeInteger operand1 = HugeInteger.parseHugeInteger( first );
86 HugeInteger operand2 = HugeInteger.parseHugeInteger( second );
87 HugeInteger result = new HugeInteger(); // stores difference
88
Declare that method
89 // subtract bottom digit from top digit
subtract is a web
90 for ( int i = 0; i < MAXIMUM; i++ ) method
91 {
92 // if the digit in operand1 is smaller than the corresponding
93 // digit in operand2, borrow from the next digit
94 if ( operand1.number[ i ] < operand2.number[ i ] )
95 operand1.borrow( i );
96
97 // subtract digits
98 result.number[ i ] = operand1.number[ i ] - operand2.number[ i ];
99 } // end for
100
101 return result.toString();
102 } // end WebMethod subtract
103
104 // borrow 1 from next digit 11
105 private void borrow( int place )
106 {
107 if ( place >= MAXIMUM )
108 throw new IndexOutOfBoundsException();
109 else if ( number[ place + 1 ] == 0 ) // if next digit is zero
110 borrow( place + 1 ); // borrow from next digit
111
112 number[ place ] += 10; // add 10 to the borrowing digit
113 --number[ place + 1 ]; // subtract one from the digit to the left
114 } // end method borrow
115
116 // WebMethod that returns true if first integer is greater than second
117 @WebMethod( operationName = "bigger" )
118 public boolean bigger( @WebParam( name = "first" ) String first,
119 @WebParam( name = "second" ) String second )
120 {
Declare that method
121 try // try subtracting first from second
bigger is a web
122 {
method
123 String difference = subtract( first, second );
124 return !difference.matches( "^[0]+$" );
125 } // end try
126 catch ( IndexOutOfBoundsException e ) // first is less than second
127 {
128 return false;
129 } // end catch
130 } // end WebMethod bigger
131
132 // WebMethod that returns true if the first integer is less than second 12
Outline
Declare that method
133 @WebMethod( operationName = "smaller" )
smaller is a web
134 public boolean smaller( @WebParam( name = "first" ) String first, method
135 @WebParam( name = "second" ) String second )
136 {
137 return bigger( second, first );
138 } // end WebMethod smaller
139
140 // WebMethod that returns true if the first integer equals the second
Declare that method
141 @WebMethod( operationName = "equals" ) equals is a web
142 public boolean equals( @WebParam( name = "first" ) String first, method

143 @WebParam( name = "second" ) String second )


144 {
145 return !( bigger( first, second ) || smaller( first, second ) );
146 } // end WebMethod equals
147 } // end class HugeInteger
13

Publishing the HugeInteger Web Service


from Netbeans
 Netbeans handles all the details of building and deploying a web service for
you
– Includes creating the framework required to support the web service

 To build project
– Right click the project name in the Netbeans Projects tab
– Select Build Project

 To deploy
– Select Deploy Project
– Deploys to the server you selected during application setup
– Also builds the project if it has changed and starts the application server if it is not
already running

 To Execute
– Select Run Project
– Also builds the project if it has changed and starts the application server if it is not
already running

 To ensure a clean re-build of the entire project


– Select Clean Project or Clean and Build Project
14

Pop-up menu that appears when you right click a project


name in the Netbeans Projects tab.
15
Testing the HugeInteger Web Service with Sun Java
System Application Server’s Tester Web page

 Sun Java System Application Server


– Can dynamically create a Tester web page for testing a web
service’s methods from a web browser
– Enable this feature via the project’s Run options

 To display the Tester web page


– Run the web application from Netbeans, or
– Type web service’s URL in browser’s address field followed by ?
Tester

 Web server must be running for a client to access a web


service
– If Netbeans launches the application server for you, the server
will shut down when you close Netbeans
– To keep it running, launch it independently of Netbeans
16

Tester web page created by Sun Java System Application Server for the HugeInteger web service.
17

Testing HugeInteger’s add method.


18

Describing a Web Service with the Web Service


Description Language (WSDL)

 To consume a web service


– Must know where to find the web service
– Must be provided with the web service’s description
 Web Service Description Language (WSDL)
– Describe web services in a platform-independent manner
– The server generates a web service’s WSDL dynamically for you
– Client tools parse the WSDL to create the client-side proxy class
that accesses the web service
 To view the WSDL for a web service
– Type URL in the browser’s address field followed by ?WSDL or
– Click the WSDL File link in the Sun Java System Application
Server’s Tester web page
19

Consuming a Web Service


 Web service client can be any type of application
or even another web service

 Web service reference


– Enables a client application to consume a web service
– Defines the client-side proxy class
20

A portion of the .wsdl file for the HugeInteger web service.


21

Creating a Client in Netbeans to Consume the


HugeInteger Web Service
 When you add a web service reference
– IDE creates and compiles the client-side artifacts—the framework
of Java code that supports the client-side proxy class

 Client calls methods on a proxy object


– Proxy uses client-side artifacts to interact with the web service

 To add a web service reference


– Right click the client project name in the Netbeans Projects tab
– Select New > Web Service Client…
– Specify the URL of the web service’s WSDL in the dialog’s WSDL
URL field
22

Creating a Client in Netbeans to Consume the


HugeInteger Web Service
 Netbeans uses the WSDL description to generate the
client-side proxy class and artifacts
 Netbeans copies the web service’s WSDL into a file in
your project
– Can view this file from the Netbeans Files tab
– Expand the nodes in the project’s xml-resources folder.
 To update client-side artifacts and client’s WSDL copy
– Right click the web service’s node in the Netbeans Projects tab
– Select Refresh Client
 To view the IDE-generated client-side artifacts
– Select the Netbeans Files tab
– Expand the project’s build folder
23

New Web Service Client dialog.


24

Netbeans Project tab after adding a web service reference to the project.
25

Locating the HugeIntegerService.wsdl file in the Netbeans Files tab.


26

Viewing the HugeInteger web service’s client-side artifacts generated by Netbeans.


1 // Fig. 28.11: UsingHugeIntegerJFrame.java 27
2 // Client desktop application for the HugeInteger web service. Outline
3 package com.deitel.jhtp7.ch28.hugeintegerclient;
4
5 // import classes for accessing HugeInteger web service's proxy
UsingHugeIntegerJ
6 import com.deitel.jhtp7.ch28.hugeintegerclient.HugeInteger; Frame.java
7 import com.deitel.jhtp7.ch28.hugeintegerclient.HugeIntegerService;
8 (1 of 10 )
9 import javax.swing.JOptionPane; // used to display errors to the user
10
11 public class UsingHugeIntegerJFrame extends javax.swing.JFrame Declare variables
12 { used to obtain and
13 private HugeIntegerService hugeIntegerService; // used to obtain proxy access the proxy
14 private HugeInteger hugeIntegerProxy; // used to access the web service
object
15
16 // no-argument constructor
17 public UsingHugeIntegerJFrame()
18 {
19 initComponents();
20
21 try
22 {
23 // create the objects for accessing the HugeInteger web service
24 hugeIntegerService = new HugeIntegerService(); Obtain the proxy
25 hugeIntegerProxy = hugeIntegerService.getHugeIntegerPort();
object
26 }
27 catch ( Exception exception ) 28
28 { Outline
29 exception.printStackTrace();
30 }
31 } // end UsingHugeIntegerJFrame constructor UsingHugeIntegerJ
32 Frame.java
33 // The initComponents method is autogenerated by Netbeans and is called
34 // from the constructor to initialize the GUI. This method is not shown
(2 of 10 )
35 // here to save space. Open UsingHugeIntegerJFrame.java in this
36 // example's folder to view the complete generated code (lines 37-153).
37
154 // invokes HugeInteger web service's add method to add HugeIntegers
155 private void addJButtonActionPerformed(
156 java.awt.event.ActionEvent evt )
157 {
158 String firstNumber = firstJTextField.getText();
159 String secondNumber = secondJTextField.getText();
160
161 if ( isValid( firstNumber ) && isValid( secondNumber ) )
162 {
163 try
164 { Use the proxy to
165 resultsJTextArea.setText( invoke web method
166 hugeIntegerProxy.add( firstNumber, secondNumber ) ); add
167 } // end try
168 catch ( Exception e ) 29
169 { Outline
170 JOptionPane.showMessageDialog( this, e.toString(),
171 "Add method failed", JOptionPane.ERROR_MESSAGE );
172 e.printStackTrace(); UsingHugeIntegerJ
173 } // end catch Frame.java
174 } // end if
(3 of 10 )
175 } // end method addJButtonActionPerformed
176
177 // invokes HugeInteger web service's subtract method to subtract the
178 // second HugeInteger from the first
179 private void subtractJButtonActionPerformed(
180 java.awt.event.ActionEvent evt )
181 {
182 String firstNumber = firstJTextField.getText();
183 String secondNumber = secondJTextField.getText();
184 Use the proxy to
185 if ( isValid( firstNumber ) && isValid( secondNumber ) ) invoke web method
186 { subtract
187 try
188 {
189 resultsJTextArea.setText(
190 hugeIntegerProxy.subtract( firstNumber, secondNumber ) );
191 } // end try
192 catch ( Exception e ) 30
193 { Outline
194 JOptionPane.showMessageDialog( this, e.toString(),
195 "Subtract method failed", JOptionPane.ERROR_MESSAGE );
196 e.printStackTrace();
UsingHugeIntegerJ
197 } // end catch
Frame.java
198 } // end if
199 } // end method subtractJButtonActionPerformed (4 of 10 )
200
201 // invokes HugeInteger web service's bigger method to determine whether
202 // the first HugeInteger is greater than the second
203 private void biggerJButtonActionPerformed(
204 java.awt.event.ActionEvent evt )
205 {
206 String firstNumber = firstJTextField.getText();
207 String secondNumber = secondJTextField.getText();
208
209 if ( isValid( firstNumber ) && isValid( secondNumber ) )
210 {
211 try
212 {
213 boolean result =
214 hugeIntegerProxy.bigger( firstNumber, secondNumber ); Use the proxy to
215 resultsJTextArea.setText( String.format( "%s %s %s %s", invoke web method
216 firstNumber, ( result ? "is" : "is not" ), "greater than",
bigger
217 secondNumber ) );
218 } // end try
219 catch ( Exception e ) 31
220 { Outline
221 JOptionPane.showMessageDialog( this, e.toString(),
222 "Bigger method failed", JOptionPane.ERROR_MESSAGE );
223 e.printStackTrace();
UsingHugeIntegerJ
224 } // end catch
Frame.java
225 } // end if
226 } // end method biggerJButtonActionPerformed (5 of 10 )
227
228 // invokes HugeInteger web service's smaller method to determine
229 // whether the first HugeInteger is less than the second
230 private void smallerJButtonActionPerformed(
231 java.awt.event.ActionEvent evt )
232 {
233 String firstNumber = firstJTextField.getText();
234 String secondNumber = secondJTextField.getText();
235
236 if ( isValid( firstNumber ) && isValid( secondNumber ) )
237 { Use the proxy to
238 try invoke web method
239 { smaller
240 boolean result =
241 hugeIntegerProxy.smaller( firstNumber, secondNumber );
242 resultsJTextArea.setText( String.format( "%s %s %s %s",
243 firstNumber, ( result ? "is" : "is not" ), "less than",
244 secondNumber ) );
245 } // end try
246 catch ( Exception e ) 32
247 { Outline
248 JOptionPane.showMessageDialog( this, e.toString(),
249 "Smaller method failed", JOptionPane.ERROR_MESSAGE );
250 e.printStackTrace();
UsingHugeIntegerJ
251 } // end catch
Frame.java
252 } // end if
253 } // end method smallerJButtonActionPerformed (6 of 10 )
254
255 // invokes HugeInteger web service's equals method to determine whether
256 // the first HugeInteger is equal to the second
257 private void equalsJButtonActionPerformed(
258 java.awt.event.ActionEvent evt )
259 {
260 String firstNumber = firstJTextField.getText();
261 String secondNumber = secondJTextField.getText();
262
263 if ( isValid( firstNumber ) && isValid( secondNumber ) )
264 { Use the proxy to
265 try invoke web method
266 { equals
267 boolean result =
268 hugeIntegerProxy.equals( firstNumber, secondNumber );
269 resultsJTextArea.setText( String.format( "%s %s %s %s",
270 firstNumber, ( result ? "is" : "is not" ), "equal to",
271 secondNumber ) );
272 } // end try
273 catch ( Exception e ) 33
274 { Outline
275 JOptionPane.showMessageDialog( this, e.toString(),
276 "Equals method failed", JOptionPane.ERROR_MESSAGE );
UsingHugeIntegerJ
277 e.printStackTrace();
Frame.java
278 } // end catch
279 } // end if (7 of 10 )
280 } // end method equalsJButtonActionPerformed
281
282 // checks the size of a String to ensure that it is not too big
283 // to be used as a HugeInteger; ensure only digits in String
284 private boolean isValid( String number )
285 {
286 // check String's length
287 if ( number.length() > 100 )
288 {
289 JOptionPane.showMessageDialog( this,
290 "HugeIntegers must be <= 100 digits.", "HugeInteger Overflow",
291 JOptionPane.ERROR_MESSAGE );
292 return false;
293 } // end if
294
295 // look for nondigit characters in String 34
296 for ( char c : number.toCharArray() )
Outline
297 {
298 if ( !Character.isDigit( c ) )
299 {
300 JOptionPane.showMessageDialog( this, UsingHugeIntegerJ
301 "There are nondigits in the String", Frame.java
302 "HugeInteger Contains Nondigit Characters",
303 JOptionPane.ERROR_MESSAGE ); (8 of 10 )
304 return false;
305 } // end if
306 } // end for
307
308 return true; // number can be used as a HugeInteger
309 } // end method validate
310
311 // main method begins execution
312 public static void main( String args[] )
313 {
314 java.awt.EventQueue.invokeLater(
315 new Runnable()
316 {
317 public void run()
318 {
319 new UsingHugeIntegerJFrame().setVisible( true );
320 } // end method run
321 } // end anonymous inner class
322 ); // end call to java.awt.EventQueue.invokeLater
323 } // end method main
324
325 // Variables declaration - do not modify 35
326 private javax.swing.JButton addJButton;
Outline
327 private javax.swing.JButton biggerJButton;
328 private javax.swing.JLabel directionsJLabel;
329 private javax.swing.JButton equalsJButton;
330 private javax.swing.JTextField firstJTextField; UsingHugeIntegerJ
331 private javax.swing.JScrollPane resultsJScrollPane; Frame.java
332 private javax.swing.JTextArea resultsJTextArea;
333 private javax.swing.JTextField secondJTextField; (9 of 10 )
334 private javax.swing.JButton smallerJButton;
335 private javax.swing.JButton subtractJButton;
336 // End of variables declaration
337 } // end class UsingHugeIntegerJFrame
36

You might also like