corner imagecorner image
FeaturesPluginsDocs & SupportCommunityPartners

End-to-End Binary SOAP Attachment 3: Creating the Web Service

Download the sample

The goal of this tutorial is to create a web application that contains a web service. The web service should delegate to the previously-created EJB module for the retrieval of images. Therefore, you need to put the EJB module on the web application's classpath.

Lessons In This Tutorial

Content on this page applies to NetBeans IDE 6.5
  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

Creating the Web Service

In the following procedure, you create the web service for this learning trail. The web service uses the previously created EJB module to retrieve binary data.

To create the web service:

  1. Choose File > New Project (Ctrl-Shift-N). The New Project wizard appears. Select Java Webfrom the Web category. Click Next. The Name and Location page opens.
  2. Type FlowerService in the Project Name field. Select the location you want for this project—use the same location for the other projects you create for this end-to-end application. Keep the default settings for the other options and click Next. The Server and Settings page opens.
  3. Select the application server and Java EE version. This must be the same for all projects in the end-to-end application. Click Finish. The IDE creates a new web application project.
  4. Put the EJB module on the web application project's classpath, so that the web service you are creating has access to the EJB module. First, right-click the web application's Libraries node and choose Add Project, as shown below.


    Libraries node context menu showing Add Libraries

    Next, browse to the EJB module and select it. You should now see a new node, for the EJB module, added to the web application's Libraries node, as below.


    Libraries node showing new FlowerAlbum EJB module sub-node

  5. Right-click the FlowerService node and choose New > Web Service. Alternatively, choose New > Other and then select Web Service under Web Services in the New File wizard. The New Web Service wizard opens.
  6. In the New Web Service wizard, type FlowerService in Web Service Name and flower.album in Package Name. Select Create Web Service from Existing Session Bean and then browse to the EJB module and select it, as below.


    Result

  7. Click OK in the Browse Enterprise Bean dialog. You return to the New Web Service wizard. Click Finish. The IDE adds the infrastructure of a web service to your application, which includes stubs for the methods obtained from the EJB module, as shown here.


    Java source view of the new Flower Service

  8. Click the Design toggle button in the top left corner of the editor. The Web Service Visual Designer is shown, as below.


    Web service designer GUI view of the new Flower Service

    You can use the Web Service Visual Designer to see the structure of your web service in one glance. In addition, you can add functionality to your web service by clicking buttons such as Add Operation. By using the Quality of Service section, you can very easily enable advanced features, such as web service security.

  9. Click the Source toggle button to switch back to the Source view. Rewrite the class so that it looks as follows.
    import java.awt.Image;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import javax.ejb.EJB;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReadParam;
    import javax.imageio.ImageReader;
    import javax.imageio.stream.ImageInputStream;
    import javax.jws.WebMethod;
    import javax.jws.WebService; @WebService(serviceName = "FlowerService") public class FlowerService { @EJB private FlowerRemote ejbRef; @WebMethod(operationName = "getFlower") public Image getFlower(String name) throws IOException { byte[] bytes = ejbRef.getFlower(name); return getImage(bytes, false); } @WebMethod(operationName = "getThumbnails") public List<Image> getThumbnails() throws IOException { List<byte[]> flowers = ejbRef.allFlowers(); List<Image> flowerList = new ArrayList<Image>(flowers.size()); for (byte[] flower : flowers) { flowerList.add(getImage(flower, true)); } return flowerList; } private Image getImage(byte[] bytes, boolean isThumbnail) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(bytes); Iterator readers = ImageIO.getImageReadersByFormatName("jpeg"); ImageReader reader = (ImageReader) readers.next(); Object source = bis; // File or InputStream ImageInputStream iis = ImageIO.createImageInputStream(source); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); if (isThumbnail) { param.setSourceSubsampling(4, 4, 0, 0); } return reader.read(0, param); } }

The web service is now complete, delegating to the EJB module, and exposing its images.

Next step:

Testing the Web Service


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.

Companion
Projects:
MySQL Database Server   GlassFish Community: an Open Source Application Server   Open Solaris  Open JDK: an Open SourceJDK   Mobile & Embedded Community     Sponsored by 
Sponsored by Sun Microsystems