Defining a web service returning a value

This recipe explains how to define a web service with operations that return a value to the client. The methods that are returning values are used in synchronous and asynchronous communication. The best practice when using multiple parameters in web service methods is to define the complex elements and their corresponding complex objects. This also applies to the returning types.

However, we don't always have the ability to define by the best practice. Indeed, when integrating existing applications, we usually bump into legacy methods which we are not able to change.

Getting ready

We will use the book library example that we have used throughout the chapter and enable one of the non-exposed class methods to become a web service operation that returns a value to the client.

How to do it…

The steps involved in defining a web service methods that return values are as follows:

  1. In JDeveloper, open the BookLibrary.java interface class. In front of the addBook method, add the @WebMethod annotation and save the file.
    @WebMethod public int addBook(int id, String Author, String Title, int Year);
  2. Now open the BookLibraryImpl.java implementation class. Remove the annotation from the addBook method as shown in the following code:
    @WebMethod(exclude = true)
    public int addBook(int id, String Author, String Title, int Year) {
  3. Now we have a method that returns a value (book id) to the client.

How it works…

Let's examine the WSDL document that describes the newly defined operation. The operation consists of an input and output message. No fault message is defined. The following is the code:

<operation name="addBook">
  <input message="tns:addBook"/>
  <output message="tns:addBookResponse"/>
</operation>

The input message consists of four parameters defining the book as shown in the following code:

<xs:complexType name="addBook">
  <xs:sequence>
    <xs:element name="arg0" type="xs:int"/>
    <xs:element name="arg1" type="xs:string" minOccurs="0"/>
    <xs:element name="arg2" type="xs:string" minOccurs="0"/>
    <xs:element name="arg3" type="xs:int"/>
  </xs:sequence>
</xs:complexType>

The output (response) message is more interesting. This message consists of one parameter defining the ID of the added book as shown in the following code:

<xs:complexType name="addBookResponse">
  <xs:sequence>
    <xs:element name="return" type="xs:int"/>
  </xs:sequence>
</xs:complexType>

Note that the web service operation now returns the value of type integer and holds the ID of the added book.

See also

To learn how to publish and test a web service, refer to the two upcoming recipes.

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

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