Defining a response wrapper for a web service

This recipe explains how to define a response wrapper for a web service. Similar to the request wrapper, we can also define a response wrapper for a web service operation. As with the request wrapper, the response wrapper is used by the marshalling process to provide proper conversion between Java code and XML content at runtime.

Getting ready

In this recipe, we will amend the example from the Defining a request wrapper for a web service recipe.

How to do it…

We start this recipe in a similar way to how we started the Defining a request wrapper for a web service recipe. Create a new class named AuthoriseResponse in JDeveloper. We omit the getter and setter methods, so the final code of the class is as follows:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AuthoriseResponse", propOrder = {
    "response"
})
public class AuthoriseResponse {
  
  @XmlElement(name = "return")
  private TransactionResponse response;
 
    public AuthoriseResponse() {
  }
}

We annotate the class to define the XML structure of the response message. We also annotate the private member response to become a return XML element.

Tip

Note that a prerequisite for the member of a response wrapper name is return. As return is a reserved word in Java language, we must annotate the member variable with the @XmlElement annotation.

In cases where we don't specify the return XML element, we receive the following error at deployment time:

javax.xml.bind.JAXBException: return is not a valid property on class org.packt.cc.business.AuthoriseResponse

How it works…

As with the request wrapper, we notify the changes in the WSDL document in the response wrapper.

Without the @ResponseWrapper annotation, the response for our operation in WSDL looks like the following snippet:

<xs:element name="AuthoriseCreditCardResponse" type="tns:AuthoriseCreditCardResponse"/>

<xs:complexType name="AuthoriseCreditCardResponse">
 <xs:sequence>
  <xs:element name="return" type="tns:transactionResponse" minOccurs="0"/>
 </xs:sequence>
</xs:complexType>

The response message consists of an XML element that holds the response information. With the annotated source code, the WSDL document is modified to the following:

<xs:element name="authResponse" nillable="true" type="ns1:AuthoriseResponse"/>

<xs:complexType name="AuthoriseResponse">
 <xs:sequence>
  <xs:element name="return" type="tns:transactionResponse" minOccurs="0"/>
 </xs:sequence>
</xs:complexType>

The response element is created in a separate XML schema, as we defined the namespace to be different from the web service. Also, the name of the XML type that holds the response information has changed from AuthoriseCreditCardResponse to AuthoriseResponse.

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

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