corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Web Service Passing Binary Data, pt 5: Modifying the Schema and WSDL Files

Download the sample

In this section, you add the WSDL file and schema file to our application. Then you modify them to interpret arrays of bytes as Images. You also adjust various parts of the application to correctly locate the schema and WSDL file. In the process, you are introduced to various tools in the IDE that help you with WSDL and Schema files.

You can apply the procedure in this section to any JAX-WS web service, to pass any MIME type as binary data. Starting with a web service that passes binary data, as you created in Lessons 2 and 3, you customize the service's WSDL and XML schema. In the customized XML schema file, you add an expectedContentTypes="mime_type" attribute to the return element for the binary data. This attribute informs the client that it should map the binary data to a Java type (as per MIME > Java type mapping) instead of to an array of bytes. In this tutorial, you map the binary data to java.awt.Image, but you can map the binary data to any of the Java types given in the JAXB 2.0 specification, as described in the JAX-WS Users Guide.

Lessons In This Tutorial

  1. Overview
  2. Creating the EJB Module
  3. Creating the Web Service
  4. Testing the Web Service
  5. => Modifying the Schema and WSDL Files to Pass Binary Data
  6. Creating the Swing Client

Modifying the Schema File and WSDL Files to Pass Binary Data

In the following procedure, you create modified WSDL and XML Schema files for the web service that you created in a previous tutorial. The modified WSDL and Schema files enable the web service and the clients that consume it to parse JPEG image data that is passed as binary data.

To modify the WSDL and Schema files:

  1. In the Projects window, expand the FlowerService web application node until you reach the WEB-INF node.
    Flower Service project nodes with WEB-INF directory
  2. Right-click the WEB-INF node and choose New > Folder. Name the folder wsdl.
  3. Expand the Web Services node and right-click the FlowerService node. Choose Generate and Copy WSDL...
    Context menu of the FlowerService node with Generate and Copy WSDL selected
  4. The Generate and Copy WSDL dialog opens with a navigation tree. Navigate to Flower Service > web > WEB-INF > wsdl and click OK.
    Generate and Copy WSDL dialog with navigation tree, showing Flower Service/web/WEB-INF/wsdl chosen

    You now see FlowerService.wsdl and FlowerService_schema1.xsd in the wsdl node.

    Projects window showing the copied wsdl and schema files
  5. Explicitly make the application server use your own version of the WSDL file. Otherwise the application server will generate its own WSDL file. Open FlowerService.java and locate the @WebService annotation. Add to this annotation the parameter wsdlLocation="WEB-INF/wsdl/FlowerService.wsdl" as shown below:
    @WebService(serviceName = "FlowerService",
                wsdlLocation = "WEB-INF/wsdl/FlowerService.wsdl")
  6. Modify the schema file so that it specifies the expected content type of the return element. To identify the return element in the schema file, open the schema file and find the complex types getThumbnailResponse and getFlowerResponse:
    <xs:complexType name="getThumbnailsResponse">
    <xs:sequence>
    <xs:element name="return" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="getFlowerResponse">
    <xs:sequence>
    <xs:element name="return" type="xs:base64Binary" minOccurs="0"/>
    </xs:sequence>
    </xs:complexType>
  7. Add the following attributes to both return elements (<xs:element name="return".../>):.
    xmime:expectedContentTypes="image/jpeg" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"

    You should now see the following in the same lines.

    <xs:complexType name="getThumbnailsResponse">
    <xs:sequence>
    <xs:element name="return" type="xs:base64Binary" minOccurs="0" maxOccurs="unbounded" xmime:expectedContentTypes="image/jpeg" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="getFlowerResponse">
    <xs:sequence>
    <xs:element name="return" type="xs:base64Binary" minOccurs="0" xmime:expectedContentTypes="image/jpeg" xmlns:xmime="http://www.w3.org/2005/05/xmlmime"/>
    </xs:sequence>
    </xs:complexType>
  8. Now, when you redeploy the web service to the Tester application, and invoke one of the operations, you see that an image is correctly returned:
    Java application with consumed ws

Now that the Tester application has confirmed that images are correctly being returned, we can create our Swing client to retrieve and display them.

Note: You can find correctly modified versions of the WSDL and Schema files in the downloaded sample project. Both files are in the folder web/WEB-INF/wsdl.

Next Step:

Creating the Swing Client

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.