Sample Application using JAX-WS, JSF, Spring, and Java
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.
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;">Withthe
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 thatusing the
style="font-weight: bold;">
@PersistenceContext
style="color: rgb(153, 0, 0);"> annotation is the sameway 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
@WebServiceannotation 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;">CatalogWeb Service
style="font-weight: bold; color: rgb(0, 102, 0);">getItemsmethod to get the
style="font-weight: bold; color: rgb(0, 102, 0);">Itemsfor 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: .java |
|
The code below shows the
style="color: rgb(0, 102, 0); font-weight: bold;">Itementity 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: .java |
@Id
|
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 WSDLcontract 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 classname defaults to the
style="font-weight: bold; color: rgb(0, 0, 153);">portTypename and the
style="color: rgb(0, 102, 0); font-weight: bold;"> style="font-weight: bold; color: rgb(0, 102, 0);">getItems methodname defaults to the
style="color: rgb(0, 0, 153); font-weight: bold;">operationname (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 |
|
style="font-weight: bold; color: rgb(0, 102, 0);">Hereis part of the generated xml Schema for the WSDL getItems response
message:
| Code Sample from: CatalogService.xsd |
|
and the corresponding generated JAXB class:
Code Sample from: .java |
|
JAX-WS delegates all data binding functionality to JAXB 2.0:
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.
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
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<ns1:getItems>
<arg0>0</arg0>
<arg1>2</arg1>
</ns1:getItems>
</soapenv:Body>
</soapenv:Envelope>
SOAP Response
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:getItemsResponse>
<return>
<address>
<addressid>1</addressid>
<city>Mountain View</city>
<latitude>37.3857400000</latitude>
<longitude>-122.0839730000</longitude>
<state>CA</state>
<street1>Castro St</street1>
<street2/>
<zip>94040</zip>
</address>
<description>super friendly</description>
<itemid>1</itemid>
<name>Friendly Cat</name>
<numberofvotes>3</numberofvotes>
<price>307.10</price>
<totalscore>15</totalscore>
</return>
<return>
<address>
<addressid>2</addressid>
<city>Foster City</city>
<latitude>37.5469350000</latitude>
<longitude>-122.2639780000</longitude>
<state>CA</state>
<street1>Shell Blvd & Beach Park Blvd</street1>
<street2/>
<zip>94404</zip>
</address>
<description>A great pet</description>
<itemid>2</itemid>
<name>Friendly Cat</name>
<numberofvotes>3</numberofvotes>
<price>307.00</price>
<totalscore>15</totalscore>
</return>
</ns1:getItemsResponse>
</soapenv:Body>
</soapenv:Envelope>
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.
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: |
|
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
dataTablestyle="font-weight: bold;"> points to the current item in theJSF component iterates through the
list, each reference tostyle="color: rgb(0, 102, 0); font-weight: bold;" class="cCode">dataTableItem
list.
The
dataTable's valueis bound to the style="font-weight: bold;">
style="color: rgb(0, 102, 0); font-weight: bold;">itemsproperty
of the
ItemControllermanaged-bean
style="font-weight: bold;"> class,
style="font-weight: bold;">which is defined in thefaces-config.xml
| Code Sample from: faces-context.xml |
|
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;">DelegatingVariableResolverin 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);">itemsproperty 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; |
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 ServiceEndpoint Interface. The proxy implements the
style="color: rgb(0, 0, 153); font-weight: bold;"> style="color: rgb(255, 102, 0);">Catalog ServiceEndpoint 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;">getItemsmethod. 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 astyle="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
itemproperties are displayed with the
JSF
columncomponent:
| Code Sample from: List.jsp |
|
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 renderingone cell in
each column and displaying the
style="color: rgb(0, 102, 0);">item name, photo, andprice.
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 |
|
- 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 |
|
- For the JSF UI and Spring add the
DelegatingVariableResolver
to the faces-config.xml :
| Code Sample from: faces-config.xml |
|
Conclusion
The sample Store Catalog application demonstrates how to
expose Spring bean
methods
which use the
Java
Persistence APIs 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 Serviceusing JAX-WS.
Running the Sample Application on
Glassfish:
Setting Things Up
- Download and install
href="http://www.netbeans.info/downloads/index.php" target="_blank">NetBeans
5.5.1.
- Download and unzip
target="_blank">Spring 2.1 (download the with dependencies
version).
- 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.
- 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
- Start NetBeans and switch to the Runtime window (Ctrl+5).
Create the Spring Library in NetBeans
- Open the NetBeans Library Manager (in the Tools menu) and create
a new library called Spring. - 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 .
- Set the Sources to the Spring
srcdirectory. - Set the Javadoc to the Spring
docs\apidirectory.
Open and Run the Sample code SpringWS:
- 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<sample_install_dir>/SpringWS, and<sample_install_dir>/SpringWSClient,
where<sample_install_dir>is the directory where
you installed the sample package. For example, if you extracted the
contents toC:\on a Windows machine, then your newly
created directory should be atC:\SpringWS.
- Start the NetBeans IDE. Click Open Project in the File menu and
select theSpringWSdirectory you just unzipped. TheSpringWS
application is a NetBeans Enterprise Application Project, which is
actually comprised of two projects:SpringWSandSpringWS-war.
SpringWS-waris a Java EE Module of theSpringWS
project.SpringWS-wargenerates the war file andSpringWS
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 theSpringWS-war.
However, first click Close in the dialog. TheSpringWS
project will be in bold red meaning that it still has reference
problems. - Right click the
SpringWSproject and select Resolve
Reference Problems from the context menu. Use the Resolve Reference
Problems dialog to map theSpringWS-warmodule to its
project, which you'll find is a subdirectory beneath theSpringWS
directory.
- Right click the
SpringWSproject and select Open
Required Projects. Now that the dependencies are correct, theSpringWS-war
project will always open with theSpringWS
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.
- Add the Spring library to the
SpringWS-war. In the
Project window underSpringWS-war:
- Right click on Libraries and select Add Library.
- Select the Spring Library in the list, then click Add Library.
- Right click on Libraries and select Add Library.
- Edit the properties in the
SpringWS\setup\javadb.properties
file, as appropriate.
- Start the Java DB database as follows:
- Select Java DB Database in the Tools menu.
- Select Start Java DB Server.
- Select Java DB Database in the Tools menu.
- Build the project as follows:
- Right click the
SpringWSnode in the Projects
window.
- Select Build Project.
- Right click the
- Run the project as follows:
- Right click the
SpringWSnode in the Projects
window.
- Select Run Project.
- Right click the
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.
src="http://weblogs.java.net/blog/caroljmcdonald/testws.jpg"
height="570" width="639">
If this does not work,
- Right click the
SpringWSnode 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:
- In Netbeans Click Open Project in the File menu and
select theSpringWSClientdirectory you just unzipped.
TheSpringWSClientproject is a web
project which creates theSpringWSClient-war.
- Add the Spring library to the
SpringWSClient
. In the
Project window underSpringWSClient
:
- Right click on Libraries and select Add Library.
- Select the Spring Library in the list, then click Add Library.
- Right click on Libraries and select Add Library.
- Build the project as follows:
- Right click the
SpringWSClient
node in the Projects
window.
- Select Build Project.
- Right click the
- Run the project as follows:
- Right click the
SpringWSClient
node in the Projects
window.
- Select Run Project.
- Right click the
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/
)

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
- Login or register to post comments
- Printer-friendly version
- caroljmcdonald's blog
- 15857 reads





