ptg7068951
Defining a Service Endpoint Interface 315
If you’ve made an error and it does not overridea method—which would
happen if you used the wrong type or number of parameters—the compil-
er can catch the error.
The @WebMethodannotation indicates that a method can be called as a web
service. The SquareRootServerinterface also uses an @WebServiceannota-
tion that indicates the interface defines a service endpoint interface.
Annotations can take parameters that provide further customization.
SquareRootServerincludes one final annotation:
@SOAPBinding(style = Style.RPC)
This annotation helps define the contract between the web service and the
client programs that call the service. You learn more about this later in the
hour.
For now, it’s time to begin coding the web service. Create a new empty Java
file in NetBeans with the class name SquareRootServerand the package
name com.java24hours.ws. Enter the contents of Listing 22.1 into the file.
LISTING 22.1 The Full Text of SquareRootServer.java
1: package com.java24hours.ws;
2:
3: importjavax.jws.;
4: importjavax.jws.soap.;
5: importjavax.jws.soap.SOAPBinding.*;
6:
7: @WebService
8:
9: @SOAPBinding(style = Style.RPC)
10:
11: public interfaceSquareRootServer {
12: // get the square root of a number
13: @WebMethod doublegetSquareRoot(doubleinput);
14:
15: // get the current time and date as a string
16: @WebMethod String getTime();
17:
18: }
This class has been placed in the com.java24hours.wspackage, a design
decision that makes it easier for the web service to be deployed for other
software to access over the Internet.
Now that you’ve finished defining this interface, you’re ready to write the
code that implements its two methods: getSquareRoot()and getTime().