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.
In this recipe, we will amend the example from the Defining a request wrapper for a web service recipe.
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.
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
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
.