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.
This recipe will be based on the code we created in the Defining the service interface and Preparing the service implementation recipes.
The following are the steps required to annotate Java code for web service creation:
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: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.
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() {
The manual step of web service creation is the annotation of the web service interface. We open the BookLibrary.java
interface class:
@WebService
annotation as shown in the following code:@WebService public interface BookLibrary {
@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.