Annotating Java code for web service creation

When we identify the piece of Java code to become exposed as a web service, the decoration part needs to take place. This recipe will explain how to annotate the Java code in order to become as useful as a web service.

Getting ready

This recipe will be based on the code we created in the Defining the service interface and Preparing the service implementation recipes.

How to do it…

The following are the steps required to annotate Java code for web service creation:

  1. In the JDeveloper project, select the class (BookLibraryImpl.java) and right-click on it. Choose the Create Web Service... option. The Create Java Web Service wizard appears where we enter the Web Service Name and Port Name and then click on Next as shown in the following screenshot:
    How to do it…
  2. Accept the default options for the message format and click on Next. In the Methods step, select the methods that are candidates for the web service operations as shown in the following screenshot:
    How to do it…
  3. The rest of the steps are not relevant right now, so click on the Finish button.

How it works…

As a result of using the JDeveloper wizard to create the web service, we receive the annotated version of the POJO object.

During the process, annotations are added to the web service implementation class. Also note that the web service interface will not get annotated and requires a manual annotation step.

Let's inspect the web service implementation class (BookLibraryImpl). First, the class definition itself is annotated with the web service definition as shown in the following code:

@WebService(name = "BookLibrary", serviceName = "BookLibraryService", portName = "BookLibraryPort")
public class BookLibraryImpl implements BookLibrary {

This definition contains the name of the web service, the service name of the web service, and the name of the port type.

Note

The service name defines the name of the web service, while the port type defines a set of operations exposed for integration.

We also see annotations on top of the methods that should not be exposed as web service operations, for example, the listMemberStat method:

@WebMethod(exclude = true)
public Hashtable listMemberStat() {

There's more...

The manual step of web service creation is the annotation of the web service interface. We open the BookLibrary.java interface class:

  1. Annotate the interface with the @WebService annotation as shown in the following code:
    @WebService
    public interface BookLibrary {
  2. Further, annotate each method that will carry out an operation with the @WebMethod annotation as shown in the following code:
      @WebMethod public void borrowBook(int member_id, int book_id) throws BookAlreadyBorrowed;
      @WebMethod public void giveBackBook(int book_id) throws BookAlreadyBorrowed;
      @WebMethod public void reserveBook(int member_id, int book_id) throws BookAlreadyBorrowed;

The project hierarchy in JDeveloper is also changed. A new folder was generated during web service creation. We can see the Web Content folder inside the WEB-INF folder where the web.xml file resides as follows:

<?xml version = '1.0' encoding = 'windows-1250'?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
  <servlet>
    <servlet-name>BookLibraryPort</servlet-name>
    <servlet-class>org.packt.java.ws.BookLibraryImpl</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>BookLibraryPort</servlet-name>
    <url-pattern>/BookLibraryPort</url-pattern>
  </servlet-mapping>
</web-app>

The web.xml file shows that the web service created with JDeveloper and deployed to the Oracle WebLogic server will be exposed to the clients as a servlet.

See also

The web service defined in this recipe is now ready to be deployed and tested. Refer to the Publishing a web service recipe to learn how to deploy a web service. Also, refer to the Testing a web service recipe which explains what are the possibilities for testing a web service.

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

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