Skip to main content

Sample Application using JAX-WS, JSF, Spring, and Java

Posted by caroljmcdonald on September 28, 2007 at 8:41 PM PDT

Sample Application using JAX-WS, JSF, Spring,
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 Spring
Bean, and the Java Persistence APIs to implement
a 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/06/sample_applicat.html">Sample
Application using JSF, Spring 2.0, and Java Persistence APIs  href="http://weblogs.java.net/blog/caroljmcdonald/archive/2007/05/pagination_of_d.html">and
modified the
Catalog Spring 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, Spring, 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 Spring 2.0

The Catalog
Spring Bean  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);"> (note that
using the
 style="font-weight: bold;">
@PersistenceContext
 style="color: rgb(153, 0, 0);"> 
annotation is the same
way an
Entity Manager is injected for a EJB
3.0 Session Bean
.) 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 @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;">Blue for style="color: rgb(0, 0, 153); font-weight: bold;">Spring
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;">
     
 style="color: rgb(0, 0, 153); font-weight: bold;">  @Repository style="color: rgb(0, 0, 153); font-weight: bold;">
       
@Transactional


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

  

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

    private EntityManager em;

     

    public List  getItems(int
firstItem,int batchSize) {     

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

       q.setMaxResults(batchSize);

       q.setFirstResult(firstItem);

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

       return style="color: rgb(0, 102, 0); font-weight: bold;">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/06/sample_applicat.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;">


     

    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://localhost:8080/SpringWS-war/CatalogService?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/SpringWS-war/CatalogService" />

   


 


     

     



 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="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 src="http://weblogs.java.net/blog/caroljmcdonald/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/SpringWS-war/CatalogService?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 src="http://weblogs.java.net/blog/caroljmcdonald/testws.jpg"
height="570" width="639">




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
               

                super friendly
                1
                Friendly Cat
                3
                307.10
                15
           

           
               

                    2
                    Foster City
                    37.5469350000
                    -122.2639780000
                    CA
                    Shell Blvd & Beach Park Blvd
                   
                    94404
               

                A great pet
                2
                Friendly Cat
                3
                307.00
                15
           

       

   




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 src="http://weblogs.java.net/blog/caroljmcdonald/jaxws-ClientService.gif"
height="186" width="437">





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:


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
 
< style="color: rgb(204, 0, 0);">application>

    <variable-resolver>

      org.springframework.web.jsf.DelegatingVariableResolver

   

 


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

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

     

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

     


   
session

 

     

     



href="http://static.springframework.org/spring/docs/1.2.x/reference/webintegration.html">The 
recommended way to integrate Spring with JSF is to
configure the Spring

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

in the
faces-context.xml
style="color: rgb(204, 0, 0);">The
< style="color: rgb(204, 0, 0); font-weight: bold;">application>
<variable-resolver> style="color: rgb(204, 0, 0);">
elements in a faces-config.xml file allows a Faces-based application to
register a custom replacement class for the implementation of the
standard Faces VariableResolver
implementation. The Spring
 style="color: rgb(0, 0, 153); font-weight: bold;">DelegatingVariableResolver
  style="color: rgb(204, 0, 0);">
first delegates to the original resolver of the underlying JSF
implementation, then to the Spring root
WebApplicationContext.






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

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


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 style="font-weight: bold; color: rgb(255, 102, 0);">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="font-weight: bold; color: rgb(255, 102, 0);">Catalog style="font-weight: bold;">port = style="color: rgb(0, 0, 153); font-weight: bold;"> style="color: rgb(255, 102, 0);">service.getCatalogPort();

      model = new style="color: rgb(204, 0, 0); font-weight: bold;">ListDataModel( style="font-weight: bold;">port. style="font-weight: bold; color: rgb(255, 102, 0);">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; color: rgb(255, 102, 0);"> style="font-weight: bold;">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 getCatalogPort()
on the style="font-weight: bold;">CatalogService,
which returns the

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

service. The style="color: rgb(0, 102, 0); font-weight: bold;">ItemController can
then invoke the port’s
 style="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;"> style="color: rgb(255, 102, 0);">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/06/sample_applicat.html">this
previous blog.



Configuration of
the XML files for Spring 2.0, JSF, and JPA, running on Glassfish

  •  The style="color: rgb(204, 0, 0); font-weight: bold;">/WEB-INF/ style="color: rgb(0, 0, 153);">applicationContext.xml file
    is where you define your style="color: rgb(0, 0, 153); font-weight: bold;">Spring
    service beans, and resources. Below is the style="color: rgb(204, 0, 0); font-weight: bold;"> style="color: rgb(0, 0, 153);">applicationContext.xml for
    the sample Catalog app style="color: rgb(204, 0, 0); font-weight: bold;">.  For
    more information about configuring the Spring
    applicationContext.xml for JPA see this article: href="http://java.sys-con.com/read/366275.htm">Using the Java
    Persistence API (JPA) with Spring 2.0

Code Sample from: applicationContext.xml
  style="color: rgb(0, 0, 153); font-weight: bold;">version="1.0"
encoding="UTF-8"?> style="color: rgb(0, 0, 153); font-weight: bold;">
      xmlns="http://www.springframework.org/schema/beans" style="color: rgb(0, 0, 153); font-weight: bold;">
            
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
style="color: rgb(0, 0, 153); font-weight: bold;">
            
xmlns:aop="http://www.springframework.org/schema/aop"
style="color: rgb(0, 0, 153); font-weight: bold;">
            
xmlns:tx="http://www.springframework.org/schema/tx"
style="color: rgb(0, 0, 153); font-weight: bold;">
            
xsi:schemaLocation="
style="color: rgb(0, 0, 153); font-weight: bold;">
            
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
style="color: rgb(0, 0, 153); font-weight: bold;">
            
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
style="color: rgb(0, 0, 153); font-weight: bold;">
      http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
style="color: rgb(0, 0, 153); font-weight: bold;">
         
     


         
catalogService
"
class="service.Catalog"/>     
     

         
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
style="color: rgb(0, 0, 153); font-weight: bold;">
             
name="dataSource" ref="dataSource"/>
style="color: rgb(0, 0, 153); font-weight: bold;">
             
name="loadTimeWeaver">
style="color: rgb(0, 0, 153); font-weight: bold;">
                 
class="org.springframework.instrument.classloading.glassfish.GlassFishLoadTimeWeaver"/>
style="color: rgb(0, 0, 153); font-weight: bold;">
             
style="color: rgb(0, 0, 153); font-weight: bold;">
             
name="jpaVendorAdapter">
style="color: rgb(0, 0, 153); font-weight: bold;">
                 


                     
class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">
style="color: rgb(0, 0, 153); font-weight: bold;">
                     
style="color: rgb(0, 0, 153); font-weight: bold;">
                     


               
style="color: rgb(0, 0, 153); font-weight: bold;">
                               
value="oracle.toplink.essentials.platform.database.DerbyPlatform"
/>


                 
style="color: rgb(0, 0, 153); font-weight: bold;">
             
style="color: rgb(0, 0, 153); font-weight: bold;">
         
      
style="color: rgb(0, 0, 153); font-weight: bold;">
         
style="color: rgb(0, 0, 153); font-weight: bold;">
               
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
style="color: rgb(0, 0, 153); font-weight: bold;">
             
name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"
/>


             
value="jdbc:derby://localhost:1527/pet-catalog"
/>


             
username
"
value="app" />
style="color: rgb(0, 0, 153); font-weight: bold;">
             
password
"
value="app" />
style="color: rgb(0, 0, 153); font-weight: bold;">
         
     
style="color: rgb(0, 0, 153); font-weight: bold;">
         
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>   
     


         
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>      
     


         
class="org.springframework.orm.jpa.JpaTransactionManager">
style="color: rgb(0, 0, 153); font-weight: bold;">
             
name="entityManagerFactory" ref="entityManagerFactory"/>
style="color: rgb(0, 0, 153); font-weight: bold;">
             
name="dataSource" ref="dataSource"/>
style="color: rgb(0, 0, 153); font-weight: bold;">
         
   
style="color: rgb(0, 0, 153); font-weight: bold;">
         
  
style="color: rgb(0, 0, 153); font-weight: bold;"> style="color: rgb(0, 0, 153); font-weight: bold;"> style="color: rgb(0, 0, 153); font-weight: bold;">
         
     


     

     

     


  • For the spring framework you need to add a ContextLoaderListener
    and context
    parameter to your application's web.xml as shown below. For more
    information on configuring Spring see these references: href="http://www.thearcmind.com/confluence/display/SHJFT/Using+Spring+2+with+JSF">Using
    Spring 2 with JSF , href="http://static.springframework.org/spring/docs/1.2.x/reference/webintegration.html">Spring
    - Java/J2EE Application Framework Integrating with JavaServer Faces, 
    Advanced Configuration
    of the
    Spring MVC Framework


Code Sample from: web.xml
 

         
style="color: rgb(204, 0, 0); font-weight: bold;">
             
contextConfigLocation
style="color: rgb(204, 0, 0); font-weight: bold;">
             
/WEB-INF/applicationContext.xml
style="color: rgb(204, 0, 0); font-weight: bold;">
         
style="color: rgb(204, 0, 0); font-weight: bold;">
         
style="color: rgb(204, 0, 0); font-weight: bold;">
             
style="color: rgb(204, 0, 0); font-weight: bold;">
                 
      org.springframework.web.context.ContextLoaderListener
style="color: rgb(204, 0, 0); font-weight: bold;">
             
style="color: rgb(204, 0, 0); font-weight: bold;">
         
style="color: rgb(204, 0, 0); font-weight: bold;">
         
style="color: rgb(204, 0, 0); font-weight: bold;">
             
    
style="color: rgb(204, 0, 0); font-weight: bold;">
                 style="color: rgb(0, 0, 153);">org.springframework.web.context.request.RequestContextListener      style="color: rgb(204, 0, 0); font-weight: bold;">
             
style="color: rgb(204, 0, 0); font-weight: bold;">
         
   


   

  • For the JSF UI and Spring add the DelegatingVariableResolver 
    to the faces-config.xml : 
Code Sample from: faces-config.xml


     
 
< style="color: rgb(204, 0, 0);">application>

    <variable-resolver>

    org.springframework.web.jsf.DelegatingVariableResolver 

   

 

 <managed-bean>

    <managed-bean-name>item

      <managed-bean-class>

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

     

   
<managed-bean-scope> style="color: rgb(204, 0, 0);">session


       


     

     

     





Conclusion



The sample Store Catalog application demonstrates how to
expose Spring bean
methods
which use  the
Java
Persistence API
s as 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

 style="color: rgb(255, 102, 0);">Catalog 
Service
using JAX-WS.





Running the Sample Application on
Glassfish
:

Setting Things Up

  1. Download and install href="http://www.netbeans.info/downloads/index.php" target="_blank">NetBeans
    5.5.1.
  2. Download and unzip target="_blank">Spring 2.1 (download the with dependencies
    version).
  3. Download and install href="https://glassfish.dev.java.net/public/downloadsindex.html"
    target="_blank">GlassFish V1.

    Alternatively you can use href="http://java.sun.com/javaee/downloads/index.jsp">Sun Java System
    Application Server PE 9, Sun's binary distribution of GlassFish.
  4. Add the GlassFish server to NetBeans:


    • Start NetBeans and switch to the Runtime window (Ctrl+5).
    • Right-click the Servers node and select Add Server.
    • Select Sun Java System Application Server.
    • Browse to the location where you installed GlassFish and
      select Finish

Create the Spring Library in NetBeans

  1. Open the NetBeans Library Manager (in the Tools menu) and create
    a new library called Spring.
  2. Add the following jars to the class path:



       dist/spring.jar

       dist/weaving/spring-aspects.jar

       lib/jakarta-commons/commons-logging.jar

       lib/log4j/log4j-1.2.9.jar .


  3. Set the Sources to the Spring src directory.
  4. Set the Javadoc to the Spring docs\api directory.

Open and Run the Sample code SpringWS:

  1. Download the  href="https://techdayscode.dev.java.net/servlets/ProjectDocumentList?folderID=8050&expandFolder=8050&folderID=8050">
    Sample Application Code SpringWS.zip and SpringWSClient.zip and
    extract their contents. You should now see the newly extracted
    directory as /SpringWS, and /SpringWSClient,
    where is the directory where
    you installed the sample package. For example, if you extracted the
    contents to C:\ on a Windows machine, then your newly
    created directory should be at C:\SpringWS.


  2. Start the NetBeans IDE. Click Open Project in the File menu and
    select the SpringWS directory you just unzipped. The SpringWS
    application is a NetBeans Enterprise Application Project, which is
    actually comprised of two projects: SpringWS and SpringWS-war.
    SpringWS-war is a Java EE Module of the SpringWS
    project. SpringWS-war generates the war file and SpringWS
    generates the ear file which contains the war.



    You will see a Reference Problems dialog when you open the project.
    That's because the Enterprise Application Project stores the absolute
    location to its J2EE Modules. To resolve the reference problems, you
    need to configure the location for the SpringWS-war.
    However, first click Close in the dialog. The SpringWS
    project will be in bold red meaning that it still has reference
    problems.
  3. Right click the SpringWS project and select Resolve
    Reference Problems from the context menu. Use the Resolve Reference
    Problems dialog to map the SpringWS-war module to its
    project, which you'll find is a subdirectory beneath the SpringWS
    directory.


  4. Right click the SpringWS project and select Open
    Required Projects. Now that the dependencies are correct, the SpringWS-war
    project will always open with the SpringWS
    project. However, there are additional references problems with the web
    module because it references the Spring jar files that are needed to
    build the project.


  5. Add the Spring library to the SpringWS-war. In the
    Project window under SpringWS-war:


    • Right click on Libraries and select Add Library.
    • Select the Spring Library in the list, then click Add Library.


  6. Edit the properties in the SpringWS\setup\javadb.properties
    file, as appropriate.


  7. Start the Java DB database as follows:


    • Select Java DB Database in the Tools menu.
    • Select Start Java DB Server.


  8. Build the project as follows:


    • Right click the SpringWS node in the Projects
      window.
    • Select Build Project.


  9. Run the project as follows:


    • Right click the SpringWS node in the Projects
      window.
    • Select Run Project.

When you run the project, your browser should display the web service
client Tester
application provided by the
Glassfish Application Server at the url : 
http://localhost:8080/SpringWS-war/CatalogService?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.



testws.jpg src="http://weblogs.java.net/blog/caroljmcdonald/testws.jpg"
height="570" width="639">



If  this does not work,

  • Right click the SpringWS node in the Projects
    window.
  • Select Deploy Project.

then open the browser and use the url  
http://host:8080/CatalogService/Catalog?wsdl , this will display the
wsdl if the application deployed correctly. Then try the web service
test client url
http://localhost:8080/SpringWS-war/CatalogService?Tester .


Open and Run the Sample code WSClient:

  1. In Netbeans Click Open Project in the File menu and
    select the SpringWSClient directory you just unzipped.
    The SpringWSClient project is  a web
    project which creates the   SpringWSClient-war

       

       

     

  2.  
  3. Add the Spring library to the SpringWSClient
        
    . In the
    Project window under SpringWSClient
        
    :


    • Right click on Libraries and select Add Library.
    • Select the Spring Library in the list, then click Add Library.


  4. Build the project as follows:


    • Right click the SpringWSClient
      node in the Projects
      window.
    • Select Build Project.


  5. Run the project as follows:


    • Right click the SpringWSClient
      node in the Projects
      window.
    • Select Run Project.

When you run the project, your browser should display the
opening page
of the JSF, JAX-WS, Java Persistence API, and Spring 2.0 Sample
Application (at http://localhost:8080/SpringWSClient/
)


run.jpg

References:

  • Netbeans
    Web Services (JAX-WS) in Java EE 5 tutorial
  • Using the Java
    Persistence API (JPA) with Spring 2.0
  • href="http://java-x.blogspot.com/2006/12/data-access-with-spring-and-jpa.html">Data
    Access with Spring and JPA on Glassfish
  • href="http://blog.interface21.com/main/2006/08/07/using-jpa-in-spring-without-referencing-spring/">Using
    JPA in Spring without referencing Spring
  • href="http://static.springframework.org/spring/docs/1.2.x/reference/webintegration.html">Spring
    - Java/J2EE Application Framework Integrating with JavaServer Faces
  • href="http://www.thearcmind.com/confluence/display/SHJFT/Using+Spring+2+with+JSF">Using
    Spring 2 with JSF
  • href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-7082&yr=2007&track=6">Building
    JavaServer Faces Applications with Spring and Hibernate
  • Spring and
    Hibernate in GlassFish
  • href="http://developers.sun.com/learning/javaoneonline/j1sessn.jsp?sessn=TS-43350&yr=2007&track=3">Harnessing
    the Power of Java Platform, Enterprise Edition (Java EE) Technology
    With Spring
  • Java
    EE tutorial (includes great tutorial for JSF and JPA)
  • Pro
    EJB 3: Java Persistence API book
  • href="https://blueprints.dev.java.net/bpcatalog/ee5/persistence/index.html">Java
    BluePrints Solutions Catalog for the Java Persistence APIs
    contains a collection of topics and example applications.
  • target="bpcatalog">Java
    Persistence reference page on GlassFish Project 
  • href="http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-jsf.html">Build
    a real-world Web application with JavaServer Faces, the Spring
    Framework, and Hibernate




Related Topics >>