Through the service interface, a web service exposes functionality to the outer world. The interface methods can be simple, such as setting properties to more complex operations such as performing calculations or updating records in a database. We have described the WSDL interface in the introduction of Chapter 2, Calling Services from BPEL, however, in this recipe, we will work on the Java interface. We use the Java interface when the Java code itself will define the web service and the WSDL document of deployed web services is generated from the Java code.
We described in the Introduction section that the core of the sample is the BookLibrary
class. However, the sample is not yet implemented. To set up a proper service interface, we define an exception in the project (BookAlreadyBorrowed
). This exception will be thrown when any member of the library tries to borrow an already borrowed book. The exception extends the Java standard Exception
class and overrides the constructors as shown in the following code:
public class BookAlreadyBorrowed extends Exception {
We will not describe specifically the BookRecord
(holds information about books) and Member
(holds information about members of the library) classes, because they present simple data access objects and are as such of no relevance to this recipe.
Following are the steps we need to perform to define the web service interface. We start by defining the Java interface class:
public interface BookLibrary { public int addMember(int id, String name, String surname, String address, Date membershipExpiry); public int addBook(int id, String Author, String Title, int Year); public void borrowBook(int member_id, int book_id) throws BookAlreadyBorrowed; public void giveBackBook(int book_id); public void reserveBook(int member_id, int book_id) throws BookAlreadyBorrowed; public Hashtable listMemberStat(); public Hashtable listBookStat(); }
In the Java interface, we defined the methods that perform the operations of the book library. The following table will define their description and behavior:
In this recipe, we defined the interface of the book library as a Plain Old Java Object (POJO). To expose the interface as a web service, we need to decorate the interface with annotations. Refer to the Annotating Java code for web service creation recipe to learn more about annotating an interface.