|
|
|||||||||||||||||||||||||
Carol McDonald's Blog
«Sample Application using JAX-WS, JSF, EJB 3.0, and Java |
Main
| Sample Catalog Application using using JRuby and Rails »
Sample Application using JAX-WS, JSF, Spring, and JavaPosted by caroljmcdonald on September 28, 2007 at 08:41 PM | Comments (12)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). | |||||||||||||||||||||||||
Code Sample from: Catalog.java |
@WebService @Repository |
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 this
previous blog.Code Sample from: Item.java |
@Id
// getters and setters for
other item attributes |
Catalog WSDL
contract at this URL:
http://localhost:8080/SpringWS-war/CatalogService?wsdl
. Below is part of the WSDL, you can see that the Catalog class
name defaults to the portType
name and the getItems method
name defaults to the operation
name (defaults can be changed using annotations, see the
Java EE tutorial for more information).| Code Sample from: CatalogService.wsdl |
<portType
name="Catalog"> |
Here
is part of the generated xml Schema for the WSDL getItems response
message:| Code Sample from: CatalogService.xsd |
<xs:complexType name="getItemsResponse"> |
Code Sample from: GetItemResponse.java |
@XmlRootElement(name
= "getItemResponse") |
<?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>
<?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>
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.List.jsp
web page the dataTable is defined as shown below: Code Sample from: List.jsp |
|
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 dataTableItem points to the current item in the
list.
dataTable's value
is bound to the items
property
of the ItemController
managed-bean class, which is defined in the
faces-config.xml | Code Sample from: faces-context.xml |
<application> <managed-bean> |
DelegatingVariableResolver
in the
faces-context.xml. The <application>
<variable-resolver>
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 DelegatingVariableResolver
first delegates to the original resolver of the underlying JSF
implementation, then to the Spring root
WebApplicationContext. items
property is defined as shown below (the orange color
highlights the JAX-WS
dynamic proxy classes and methods):| Code Sample from: ItemController.java |
public class ItemController { @WebServiceRef(wsdlLocation = "http://localhost:8080/CatalogService/Catalog?wsdl") private CatalogService service; public DataModel getItems() { |
getCatalogPort()
on the CatalogService,
which returns the Catalog Service
Endpoint Interface. The proxy implements the Catalog Service
Endpoint Interface defined by the Catalog
service. The ItemController can
then invoke the port’s 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 List<Item>. getItems()
method wraps a List of items, returned from the Catalog Service,
in a DataModel.
The dataTable JSF
component supports data binding to a
collection of data objects represented by a 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. item
properties are displayed with the
JSF column
component:| Code Sample from: List.jsp |
|
dataTable.
The dataTable JSF component iterates
through the list of items
(item.items) each time rendering
one cell in
each column and displaying the item name, photo, and
price. | Code Sample from: applicationContext.xml |
<?xml
version="1.0"
encoding="UTF-8"?> |
| Code Sample from: web.xml |
|
DelegatingVariableResolver
to the faces-config.xml : | Code Sample from: faces-config.xml |
<application> </managed-bean> |
Catalog Service
using JAX-WS.
Setting Things Up
Create the Spring Library in NetBeans
dist/spring.jar dist/weaving/spring-aspects.jar lib/jakarta-commons/commons-logging.jar lib/log4j/log4j-1.2.9.jar .src directory.docs\api directory. Open and Run the Sample code SpringWS:
<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 to C:\ on a Windows machine, then your newly
created directory should be at C:\SpringWS.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. SpringWS-war.
However, first click Close in the dialog. The SpringWS
project will be in bold red meaning that it still has reference
problems. 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.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. SpringWS-war. In the
Project window under SpringWS-war: SpringWS\setup\javadb.properties
file, as appropriate.SpringWS node in the Projects
window.SpringWS node in the Projects
window.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.
If this does not work,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 .
- Right click the
SpringWSnode in the Projects window.
- Select Deploy Project.
Open and Run the Sample code WSClient:
SpringWSClient directory you just unzipped.
The SpringWSClient project is a web
project which creates the SpringWSClient-war.
SpringWSClient
. In the
Project window under SpringWSClient
: SpringWSClient
node in the Projects
window.SpringWSClient
node in the Projects
window.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/ )

Hi,
Thanks for this nice tutorial.
I did exactly as you explained. By doing "Clean and Build" I'm getting following error:
[ERROR] Could not find wsdl:service in the provide WSDL(s):
file:/C:/Development_Code/JSF-WS/SpringWS/SpringWS-war/src/conf/xml-resources/web-service-references/CatalogService/wsdl/localhost_8080/SpringWS-war/CatalogService.wsdl
You need to provide atleast one WSDL with at least one service definition.
Any help would be highly appreciated.
Thanks
Posted by: lisa_facelets on October 01, 2007 at 05:11 AM
HI, I don't have this problem, so I'm not sure. Do you see this when you build the SpringWS service or the SpringWSClient? If its the Service, try deleting the @WebService line from serviceCatalog.java then fix the imports. Select the project node and select Clean. Then Add the @WebService line back to the serviceCatalog.java, fix the imports then do a build and then do a deploy. If you have this problem for the client, make sure the Glassfish server is started and the SpringWS ear is deployed. Then in the Netbeans SpringWSClient project under WebService References right click on the CatalogService and do refresh client. If that doesn't work then delete the CatalogService .Select the project node and select Clean .Right Click the SpringWSClient project node, and select new Web Service client. Enter the WSDL url http://localhost:8080/SpringWS-war/CatalogService?wsdl and the package sessionpagination and click finish. This will use the wsimport tool to generate the dynamic proxy
Posted by: caroljmcdonald on October 01, 2007 at 08:01 AM
It is quite necessary to perpetuate the institution of the "pet store"?
Similar functionality could be demonstrated as an adoption service.
For example,
http://www.peninsulahumanesociety.org/adopt/dog.html
or
http://petfinder.com/
Posted by: apmarki on October 01, 2007 at 02:08 PM
I made a sample pet store catalog because the database tables and images were already available from the blueprints java.net project, and a store catalog is useful as an example. As a further exercise you could change this into a humane society app if you think that would be interesting.
Posted by: caroljmcdonald on October 01, 2007 at 02:19 PM
Hi,
Thanks for your reply.
Its working now as expected.
Waiting for new tutorials.
Thanks again.
Posted by: lisa_facelets on October 02, 2007 at 02:10 AM
Hi carol.
First of all, i have to say that this posting is great and useful.
Since i am new to web service, this helped me a lot in understanding. I have a little issue. You have used glassfish to run the web client if my understanding is right. I thought of writing a web client to start the webservice. I am thinking to use bean shell scripts for this. I am not so comfortable in using the servlet kind of service to run the web service. Can you throw some lights on this issue. I will be grateful if you explain me with some examples.
Posted by: sensabesan on October 25, 2007 at 05:46 AM
You can use JAX-WS with Java EE or Java SE. Here is an article that explains how to use JAX-WS 2.0 With the Java SE 6 Platform :
http://java.sun.com/developer/technicalArticles/J2SE/jax_ws_2/
Posted by: caroljmcdonald on October 25, 2007 at 06:26 AM
Hi Carol,
Thanks for the immediate reply. But unfortunately, I am using Java 5.0. I think i can't migrate right now to Java 6. What all the options we have to write JAX-WS client using java 5.0
Posted by: sensabesan on October 25, 2007 at 07:03 AM
I'm so grateful for all that you've done. Thanks again for that nice essay and I would be most grateful if you would send me the latter ones....
mirc
mırc
mirç
mırç
mirc indir
chat yap
islami sohbet
dini sohbet
kelebek
kelebek sohbet
kelebek mirc
kameralı mirc
kameralı sohbet
chat yap
çet
çet odaları
sohbet kanalları
sohbet odaları
yarışma
sevgili
arkadaş
arkadaş ara
arkadaşlık
Posted by: jklmno on June 19, 2008 at 08:57 AM
|
|