Skip to main content

Sample Application using JAX-WS, JSF, EJB 3.0, and Java

Posted by caroljmcdonald on September 18, 2007 at 8:43 PM PDT

Sample Application using JAX-WS, JSF, EJB 3.0,
and Java Persistence APIs on Glassfish

eBay and Amazon provide Web Services APIs for developers who want to
provide access to these services in their web site. This Sample Store
Catalog application shows how to expose a 
Service as a Web Service for remote client applications (this example
was not modeled after the eBay or Amazon APIs).



This example demonstrates a Catalog Stateless Session
Bean, and the Java Persistence APIs to implement
Catalog
Service which provides pagination of store items,  and JAX-WS to
expose this Catalog
Service as a Web Service. A
separate example JSF JAX-WS client shows how this Catalog Web Service
can then
be used remotely in a sample Store web site.   I took this
example   href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">Pagination
of Data Sets in a Sample Application using JSF, Catalog Facade
Stateless Session, and Java Persistence APIs and modified the
Catalog Session Bean to expose its public methods as Web Services, then
I put the JSF Store UI in a separate Web Application and modified it to
use
JAX-WS to call the Catalog Web Services.



href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=8050&expandFolder=8050&folderID=8050">Download
the Sample Application Code


Explanation of the usage of  JAX-WS,  JSF, EJB 3.0, and
Java Persistence
APIs in a sample Store Catalog Application

The image below shows the Catalog Listing page, which allows a user to
page through a list of items
in a store.


listpage.jpg src="http://weblogs.java.net/blog/caroljmcdonald/archive/images/listpage.jpg">




Explanation of the Catalog Web Service which uses JAX-WS, JPA,
and EJB 3.0

The Catalog
Session EJB  uses a JPA EntityManager
Query

 style="color: rgb(204, 0, 0); font-weight: bold;">

object to return a list of
 style="font-weight: bold; color: rgb(0, 102, 0);">items
.
 style="color: rgb(204, 0, 0);">
 style="font-weight: bold; color: rgb(204, 0, 0);">
 style="color: rgb(204, 0, 0); font-weight: bold;">
With
the
 style="color: rgb(204, 0, 0);">@PersistenceContext 
annotation,
the CatalogBean uses dependency injection to lookup and obtain a
Container Managed
 style="font-weight: bold;">EntityManager
style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);"> style="color: rgb(204, 0, 0);"> style="font-weight: bold;"> style="color: rgb(204, 0, 0);">
 style="font-weight: bold; color: rgb(204, 0, 0);">

The
@Stateless
annotation marks this class as a Stateless Session EJB.
The @WebService
annotation marks this class as a web service, and causes any public
methods to be exposed as Web Services.  The example JSF Web
Service client uses the
 style="color: rgb(0, 102, 0); font-weight: bold;">Catalog

Web Service
 style="font-weight: bold; color: rgb(0, 102, 0);">getItems

method to get the
 style="font-weight: bold; color: rgb(0, 102, 0);">Items

for displaying on the Web Store UI .   (Note: style="color: rgb(204, 0, 0); font-weight: bold;">Red colors
are for Java EE
tags, annotations code,  style="color: rgb(0, 0, 153); font-weight: bold;">
and Green
for my code
or variables)


Code Sample from:
 style="color: rgb(0, 102, 0); font-weight: bold;">Catalog
.java
 

  
 style="font-weight: bold; color: rgb(0, 0, 153);"> style="color: rgb(204, 0, 0); font-weight: bold;">@WebService style="color: rgb(204, 0, 0); font-weight: bold;">
        
@Stateless


   public class style="color: rgb(0, 102, 0); font-weight: bold;">Catalog
implements CatalogService {

     

     
    style="font-weight: bold; color: rgb(204, 0, 0);">@PersistenceContext(unitName=" style="color: rgb(0, 102, 0); font-weight: bold;">PetCatalogPu")

   private style="font-weight: bold;">EntityManager em;

 

   @TransactionAttribute style="color: rgb(204, 0, 0);">(TransactionAttributeType.NOT_SUPPORTED)

   public List style="font-weight: bold; color: rgb(0, 102, 0);">getItems(int
firstItem,int batchSize)
{

        style="color: rgb(204, 0, 0); font-weight: bold;">Query q = style="color: rgb(204, 0, 0); font-weight: bold;">em.createQuery("select
object(o) from Item
as o");

        q. style="color: rgb(204, 0, 0); font-weight: bold;">setMaxResults(batchSize);

        q. style="color: rgb(204, 0, 0); font-weight: bold;">setFirstResult(firstItem);

        style="color: rgb(0, 102, 0);">List< style="color: rgb(0, 102, 0); font-weight: bold;">Item style="color: rgb(0, 102, 0);">> style="color: rgb(0, 102, 0); font-weight: bold;"> items= style="color: rgb(204, 0, 0); font-weight: bold;">q.getResultList();

        return items;

   }

   

     







The code below shows the

 style="color: rgb(0, 102, 0); font-weight: bold;">Item

entity class which maps to the  ITEM table that stores the
item instances. This is a
typical Java Persistence entity object.  For more information on
this code see href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">this
previous blog.



Code Sample from:
 style="color: rgb(0, 102, 0); font-weight: bold;">Item
.java


      @Entity

     

public class Item
implements java.io.Serializable {

     

   
@Id
  

    private String itemID;


    private String name;   

    private String description;   

    private String imageurl;    

    private BigDecimal price;

    style="color: rgb(204, 0, 0); font-weight: bold;">@OneToOne style="color: rgb(204, 0, 0);">(cascade={CascadeType.PERSIST})

    private style="color: rgb(0, 102, 0); font-weight: bold;">Address address;

     
     style="color: rgb(204, 0, 0); font-weight: bold;">@ManyToOne

    private style="color: rgb(0, 102, 0); font-weight: bold;">Product product;


     

    public Item() { }

     

    public String getItemID() {

       
return itemID;

    }


    public void setItemID(String itemID) {

       
this.itemID = itemID;

   
}


    // getters and setters for
other item attributes

    ...

}   

     

     





When this application is deployed,  the JAXB,  JAX-WS, and
WSDL files needed for the Service will be generated on the
server. You can access the

 style="color: rgb(0, 102, 0); font-weight: bold;">Catalog 
WSDL
contract at this URL: http://host:8080/CatalogService/Catalog?wsdl
.  Below is part of the WSDL, you can see that the
 style="color: rgb(0, 102, 0); font-weight: bold;">Catalog 
class
name defaults to the
 style="font-weight: bold; color: rgb(0, 0, 153);">portType

name and the
 style="color: rgb(0, 102, 0); font-weight: bold;"> 
 style="font-weight: bold; color: rgb(0, 102, 0);">getItems 
method
name defaults to the
 style="color: rgb(0, 0, 153); font-weight: bold;">operation

name (defaults can be changed using annotations, see href="http://java.sun.com/javaee/5/docs/tutorial/doc/bnayk.html">the
Java EE tutorial for more information).
 style="color: rgb(0, 102, 0); font-weight: bold;">





Code Sample from: CatalogService.wsdl


<portType
name="Catalog">

    < style="color: rgb(0, 0, 153); font-weight: bold;">operation
name="getItems">

       message="tns:getItems"/>

       message="tns:getItemsResponse"/>

   



...

 

    binding="tns:CatalogPortBinding">

      location="http://host:8080/CatalogService/Catalog" />

   


 


     

     



 style="font-weight: bold; color: rgb(0, 102, 0);">
Here
is part of the generated xml Schema for the WSDL getItems response
message:




Code Sample from: CatalogService.xsd


 

   

      type="tns:item" />

   


 


     

 

   

      type="tns:address" minOccurs="0"/>

      type="tns:sellercontactinfo" minOccurs="0"/>

      type="xs:string" minOccurs="0"/>

      type="xs:int"/>

      type="xs:string" minOccurs="0"/>

      type="xs:string" minOccurs="0"/>

      type="xs:string" minOccurs="0"/>

      type="xs:string" minOccurs="0"/>

      type="xs:int"/>

      type="xs:decimal" minOccurs="0"/>

      type="tns:product" minOccurs="0"/>

      type="xs:int"/>

   


 


     

     





and the corresponding generated JAXB class:


Code Sample from:
 style="font-weight: bold;">GetItemResponse
.java


     
@XmlRootElement(name
= "getItemResponse")

@XmlType(name = "getItemResponse")

public class GetItemResponse {

     

    @XmlElement(name = "return")

    private Item _return;

     

    public Item getReturn() {

        return this._return;

    }

     

    public void setReturn(Item _return) {

        this._return = _return;

    }

   

...

}

     

     



JAX-WS delegates all data binding functionality to JAXB 2.0:

jaxb.gif.gif style="width: 622px; height: 414px;"/>




After deployment on Glassfish you can access a web client tester
application provided by the
Glassfish Application Server at the  URL for the Web Service
followed by "?Tester"  for example : 
http://host:8080/CatalogService/Catalog?Tester. Below is the Web
Service Tester interface for the CatalogService. It provides an easy
way to call the Web Service operations from a browser.



testws.jpg





Here is an example soap request and response for the getItems
WebService operation:

SOAP Request




   
   
       
            0
            2
       

   



SOAP Response




   
       
           
               

                    1
                    Mountain View
                    37.3857400000
                    -122.0839730000
                    CA
                    Castro St
                   
                    94040
               

               
                    1
                    abc@abc.xyz
                    Sean
                    Brydon
               

                super friendly
                1
                Friendly Cat
                3
                307.10
               
                   
                        CATS
                        Loving and finicky friends
                        Cats
                   

                    Great for reducing mouse populations
                    Hairy Cat
                    feline01
               

                15
           

           
               

                   ...
               

               
                    ...
               

                2
                Fluffy Cat
               
                   
                        CATS
                         ...
                   

                    Hairy Cat
                    feline01
               

           

       

   




Explanation of the JSF Store UI which uses JAX-WS to call the
Catalog Web Service.

The JSF Store UI is a separate web application which is a JAX-WS
client.



jaxws-ClientService.gif






The List.jsp page uses a JSF dataTable
component to display a list of
catalog items. The dataTable component is useful when you want to show
a set of
results in a table.



In the List.jsp
web page the dataTable is defined as shown below:


border="1" cellpadding="2" cellspacing="0">
Code Sample from: 
List style="color: rgb(0, 0, 0);">.jsp


<h:dataTable style="color: rgb(204, 0, 0);"> style="font-weight: bold; color: rgb(204, 0, 0);">value=' style="font-weight: bold;">#{ style="color: rgb(0, 102, 0); font-weight: bold;">item.items style="font-weight: bold;">}' style="font-weight: bold; color: rgb(204, 0, 0);">var=' style="color: rgb(0, 102, 0); font-weight: bold;">dataTableItem'
border="1"

      cellpadding="2" cellspacing="0">

     

     



The value attribute of a dataTable
tag references the data to be included
in the table. The var
attribute points
to a single item in that list. As the

dataTable
JSF component iterates through the
list, each reference to style="color: rgb(0, 102, 0); font-weight: bold;" class="cCode">dataTableItem
style="font-weight: bold;"> points to the current item in the
list.




The dataTable's value
is bound to the style="font-weight: bold;">
 style="color: rgb(0, 102, 0); font-weight: bold;">items

property
of the ItemController
managed-bean  style="font-weight: bold;"> class,  style="font-weight: bold;">which is defined in the
faces-config.xml




Code Sample from: faces-context.xml


 <managed-bean>

    style="color: rgb(0, 102, 0);">item

     

         style="color: rgb(0, 102, 0); font-weight: bold;">sessionpagination.ItemController

     


   
session

 

     

     



This ItemController ManagedBean

 style="color: rgb(0, 102, 0);">items

property is defined as shown below (the style="color: rgb(0, 0, 153); font-weight: bold;">blue color
highlights the JAX-WS
dynamic proxy classes and methods
):


border="1" cellpadding="2" cellspacing="0">
Code Sample from: ItemController.java


 public class style="color: rgb(0, 102, 0); font-weight: bold;">ItemController
{



  @WebServiceRef(wsdlLocation
= "http://localhost:8080/CatalogService/Catalog?wsdl")

  private CatalogService
service;


     

     
  public  style="color: rgb(204, 0, 0); font-weight: bold;">DataModel  style="color: rgb(0, 102, 0); font-weight: bold;">getItems() {

      if (model==null  || index !=
firstItem){

         
model=getNextItems();

      }

      return this.model;

  }

  public DataModel
getNextItems() {

      style="color: rgb(0, 0, 153); font-weight: bold;">Catalog style="font-weight: bold;">port = style="color: rgb(0, 0, 153); font-weight: bold;">service.getCatalogPort();

      model = new style="color: rgb(204, 0, 0); font-weight: bold;">ListDataModel( style="font-weight: bold;">port. style="color: rgb(0, 0, 153); font-weight: bold;">getItems(
firstItem,batchSize));

      return model;

  }

     

     



The style="color: rgb(0, 102, 0); font-weight: bold;">ItemController
ManagedBean uses dependency injection to obtain a reference to the style="font-family: monospace;"> style="font-weight: bold; color: rgb(0, 0, 153);">CatalogService
JAX-WS proxy factory class, which is generated from the Catalog WSDL
file using the wsimport utility. (To see how to do this easily with
Netbeans see the  href="http://www.netbeans.org/kb/55/websvc-jax-ws.html">Netbeans Web
Services (JAX-WS) in Java EE 5 tutorial). The style="font-family: monospace;"> style="color: rgb(0, 102, 0); font-weight: bold;">ItemController
retrieves a proxy to the service by calling

 style="color: rgb(0, 0, 153); font-weight: bold;">getCatalogPort()

on the style="font-weight: bold; color: rgb(0, 0, 153);">CatalogService,
which returns the
 style="color: rgb(0, 0, 153); font-weight: bold;">Catalog 
Service
Endpoint Interface.  The proxy implements the
 style="color: rgb(0, 0, 153); font-weight: bold;">Catalog 
Service
Endpoint Interface defined by the
 style="color: rgb(0, 0, 153); font-weight: bold;">Catalog 

service. The style="color: rgb(0, 102, 0); font-weight: bold;">ItemController can
then invoke the port’s
 style="color: rgb(0, 0, 153); font-weight: bold;">getItems

method.  The dynamic proxy and jaxb classes convert the WS method
into a SOAP request and send it to the Web service's endpoint, 
receive the SOAP response, and transform the SOAP response into the
java method's return object which in this case is a
 style="color: rgb(0, 102, 0);">List< style="color: rgb(0, 102, 0); font-weight: bold;">Item style="color: rgb(0, 102, 0);">> style="color: rgb(0, 102, 0); font-weight: bold;">
.



The getItems()
method wraps a List of items, returned from the
 style="color: rgb(0, 0, 153); font-weight: bold;">Catalog 
Service, 
in a DataModel.  
The dataTable
JSF
component supports data binding to a
collection of data objects represented by a style="font-weight: bold; color: rgb(204, 0, 0);">DataModel

instance.  The data
collection underlying a DataModel instance is modeled as a collection
of row objects that can be accessed by a row index.  The APIs
provide mechanisms to position to a specified row index, and to
retrieve an object that represents the data that corresponds to the
current row index.   



The Name, Photo, and Price item
properties are displayed  with the
JSF column
component:


Code Sample from: List.jsp



  column>

     

          value="Price"/>

     


      style="color: rgb(204, 0, 0); font-weight: bold;">outputText
value="#{dataTableItem.price}"/>

 


     

     



The column tags represent columns of data in a dataTable.
The dataTable JSF  component  iterates
through the list of items
(item.

 style="color: rgb(0, 102, 0);">items
) each time rendering
one cell in
each column and displaying  the
 style="color: rgb(0, 102, 0);">item
name, photo, and
price.



For more information on
the JSF part of this code see href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">this
previous blog.





Conclusion



The sample Store Catalog application demonstrates how to
expose 
EJB 3.0  style="color: rgb(204, 0, 0); font-weight: bold;">Stateless Session EJB
methods
which use  the
Java
Persistence API
s as a Web Service operations using style="color: rgb(204, 0, 0); font-weight: bold;">JAX-WS.

The sample JSF Store UI application  demonstrates how to use 
the JSF style="font-weight: bold;"> style="color: rgb(204, 0, 0); font-weight: bold;">dataTable and
DataModel
to page through a
list
of  Items
which are retrieved from the
Catalog Service
using JAX-WS.





Running the Sample Application on
Glassfish
:

  • href="https://glassfish.dev.java.net/public/downloadsindex.html">Download
    and install GlassFish V2, following the instructions on the download
    page. Alternatively you can use Sun Java System Application Server PE
    9, Sun's GlassFish distribution.
  • Download
    and install NetBeans 5.5.1
  • href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=8050&expandFolder=8050&folderID=8050">Download
    the Sample Application Code
  • install Glassfish and Netbeans 5.5.1.  Then add the
    glassfish application server to Netbeans.

To Open and Test Run the sessionpagination Project:

  • Open the Netbeans sessionpagination project: In Netbeans under
    File Open Project... go to the directory where you unzipped the sample
    and select the sessionpagination project.
  • If you get a message that says unresolved references, right click
    on the project and select Resolve Reference Problems. Use the Resolve
    Reference Problems dialog to map the ejb and web
    modules to their
    project, which are subdirectories beneath the sessionpagination
    directory.
  • After the references are resolved, right-click the
    sessionpagination
    project and select Open Required Projects.
  • If the web module says unresolved references, right-click the
    sessionpagination-Web module and select Resolve
    Reference
    Problems:
  • Browse to the sessionpagination-ejb directory which is a
    sub-directory below the sessionpagination directory and select Open
    Project
    Folder.
  • If you don't have any resolve reference problems errors then
    ignore those steps.
  • Starts the application server, or at least connect to the
    database, because the run script for this application will also create
    the database tables, and this will fail if the database is not started.

  • Right-click the project node and choose Run Project.

    The Netbeans IDE starts the application server, builds the application,
    and opens the web context page in your browser. This application also
    has a local JSF client in the war of the application which will be
    displayed.
  • To go to the web client Tester
    application provided by the
    Glassfish Application Server use the url : 
    http://host:8080/CatalogService/Catalog?Tester. You should see the
    tester page. For the getItems operation type in integer the integers 0,
    5 as input and click on the getItems button. This will return a list
    of  items 0 through 5.

To Open and Test Run the sessionpagination-wsclient Project:

  • Open the Netbeans sessionpagination-wsclient project: In Netbeans
    under File
    Open Project... go to the directory where you unzipped the sample and
    select the sessionpagination-wsclient project.
  • Right-click the project node and choose Run Project.

    The Netbeans IDE  builds the application,
    and deploys it.
  • When you run the project, your browser should display the opening
    page
    of the application at http://localhost:8080/sessionpagination-wsclient/

index.jpg

References:






Related Topics >>

Comments

hi carol i have

hi carol
i have been trying to open the sample download link but it does not seem to open , either the adress of the link has been permenantly changed if so please send me the new address.
Since i am new with web services. Can u tell me please that the above tutorial explains both server and client app of the catalog example or there are different tutorials
thanking in advance

How to use the same JPA entities classes in a client app.?

Hi, I must consume a JAX-WS web service, in my stand-alone client application I have created a client for the web service with the Netbeans Web Service client wizard, this client application project have imported the library witch contains my JPA entites (the same that use the web service in the EJB module), I need that the web service client use these jpa entites and not the ones it generates (jaxb ones). Is this possible???

running the client (JSF) and server (Spring and JAX-WS services)

Hi, I was wondering how would you approach if I had to run the client (JSF) and the server (Spring and JAX-WS service implementations) on two different jvms. Would you recommend using the same domain model on the client side?. Would the sample work if I split up the client and service on two different servers (physical). I was not able to run the sample that way... thanks Srini

It should run fine on 2

It should run fine on 2 different servers, that's the point. The objects that get passed are in the WSDL and JAXB objects are generated in the client stubs from that . Just make sure you set the URL for the web service in the wsdl correctly , in the sample it is set for localhost

(No subject)