O'Reilly logo

Java Web Services: Up and Running by Martin Kalin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Web Services and Binary Data

In the examples so far, the underlying SOAP messages contain text that is converted, as needed, to service-appropriate types. The type conversion is mostly automatic, occurring in the JWS infrastructure without application intervention. For instance, here is the body of a SOAP request message to the countRabbits operation. The argument 45 occurs as text in the message:

<S:Body>
   <ns2:countRabbits xmlns:ns2="http://ch03.fib">
      <arg0>45</arg0>
   </ns2:countRabbits>
</S:Body>    

but is converted automatically to an int so that the service method countRabbits:

@WebMethod
public int countRabbits(int n) throws FibException {

can compute and return the Fibonacci number for integer argument n. Neither the FibClient application nor the RabbitCounter service does any explicit type conversion.

By contrast, some explicit type conversions occur at the handler level. For instance, the UUIDHandler on the client side and the UUIDValidator on the service side do explicit, if simple, type conversions. The UUIDHandler converts the UUID object to a string:

helem.addTextNode(uuid.toString());    

and the UUIDValidator does the opposite conversion:

UUID uuid = UUID.fromString(value.trim());    

The client-side ArgHandler does the most work with respect to type conversion. This logical handler uses JAX-B in this code segment:

JAXBContext jaxb_ctx = JAXBContext.newInstance("fibC");
Object payload = msg.getPayload(jaxb_ctx);
if (payload instanceof JAXBElement) {
 Object obj = ((JAXBElement) payload).getValue(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required