ptg7068951
314 HOUR 22:Creating Web Services with JAX-WS
An interface is a set of methods that provides names, arguments, and
return types but does not contain code that implements the methods. The
interface serves as a contract between objects—if an object implements an
interface, other objects know they can call all the interface’s methods on
that object.
In Hour 15, “Responding to User Input,” you had to implement the
ActionListenerinterface in any Java class that needed to receive action
events when a button was clicked.
For this project, you’re handling the other side of the contract. The
SquareRootServerinterface defines two methods that must be present in a
class that implements the web service: squareRoot(double)and getTime().
The following statements define the interface:
public interfaceSquareRootServer {
doublegetSquareRoot(doubleinput);
String getTime();
}
The method definitions in an interface are followed by a semicolon rather
than {and }characters around a block statement. Interfaces don’t define the
behavior of methods; that’s handled by classes that implement the interface.
Because these methods can be called as a JAX-WS web service, an extra
modifier must be added in front of each one, the annotation @WebMethod:
public interfaceSquareRootServer {
@WebMethod doublegetSquareRoot(doubleinput);
@WebMethod String getTime();
}
Using Annotations to Simplify Java Code
Annotations are a smarter form ofcomments that can be understood by
the Java interpreter, compiler, and programming tools. They provide a way
to define information about a program that’s not part of the program itself
but which can trigger actions when the program is compiled or run.
Annotations begin with an @sign followed by the name of the annotation.
One of the most common annotations is @Override, which indicates a
method overrides a superclass method. Here’s an example:
@Overrides public void paintComponent(Graphics comp) {
// definition of method here
}