Java Reference
In-Depth Information
Using Binary Data in a SOAP Message
Problem
You have binary data, such as an image or a PDF, that you need to pass to a web service.
Solution
Use a byte array as the parameter type.
Discussion
Using a byte array as the parameter type will allow you to read a stream of bytes on
the client and pass it straight into the method. The SOAP mechanism will encode it as
xs:base64Binary content and pass it to the service. Here is such a method as defined on the
web service:
@WebMethod
@SOAPBinding(parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public @WebResult(name="putResponse",
targetNamespace="http://ns.soacookbook.com/bin")
String
put(
@WebParam(name="putData",
targetNamespace="http://ns.soacookbook.com/bin")
byte[] binaryData) {
The matching generated schema looks like this:
<xs:element name="put" type="tns:put"></xs:element>
<xs:complexType name="put">
<xs:sequence>
<xs:element name="putData" type="xs:base64Binary"
form="qualified" nillable="true" minOccurs="0" />
</xs:sequence>
</xs:complexType>
Let's make a client in the form of a unit test that will read in some image data and post it to
the service. Here you will pass in a JPG image as the value of the parameter to the put request
on the service. The service will return an ID, which for the purposes of the unit test will be
“007”. See Example 6-17 .
Example6-17.BinaryDataTest.java
Search WWH ::




Custom Search