Defining the service interface

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.

Getting ready

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.

How to do it…

Following are the steps we need to perform to define the web service interface. We start by defining the Java interface class:

  1. At the root of the JDeveloper project, right-click and select the New... option. In the New Gallery window, select the Java Interface artifact as shown in the following screenshot:
    How to do it…
  2. Click on the OK button. The Create Java Interface window opens. Enter the Name and Package fields and click on the OK button as shown in the following screenshot:
    How to do it…
  3. Now define the following methods that we will use for the library operations:
    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();
    }

How it works…

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:

Interface method

Description

addMember

This method opens and adds new members to the book library.

addBook

This method adds new books to the book library and members can borrow it.

borrowBook

This method lets a member borrow the book from the library and the book is no longer available for borrowing. This method also checks if the book has already been borrowed.

giveBackBook

Using this method, a member returns the book to the library and the book is again available to be borrowed.

reserveBook

A member of the library can reserve the book (either by web, phone, SMS, and so on) and can come later to borrow it. This method also checks if the book has already been borrowed, since no reservation is possible for an already borrowed book.

listMemberStat

This is a convenient method that returns a list of all members.

listBookStat

This is a convenient method that returns a list of all the books and who borrowed each book.

See also

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset