Create a simple JavaEE application with XFire
Written by Administrator    Wednesday, 10 September 2008 19:16    PDF Print E-mail

 This is simple step by step tutorial on how to build an application that will display content on web pages. Everything will be done in a pedagogic simple way.The nuild is done with the Apache Maven framework instead of ant.

Below is a small list of tools used. All fancy words or strange settings will be omitted. In 90% of the applications you build you can use the default settings and they will work.

This tutorial contains these parts;
Part 1 - Create a stand alone application accessing a database with Hibernate.
Part 2 - Add web pages and access the database by calling methods on the application built in part 1. Deploy it on a server.
Part 3 - Change the application to use a datasource instead of a driver manager.
Part 4 - Add Ajax to interact with server.<br>
Part 5 - Add ejb to use as a session facade to the methods created in part 1.


I used hiberate as the orm mapping system. Nobody uses raw sql anymore, at least not professionally. Many use the new JPA standard that was introduced together with EJB3.0 (which indirectly use TopLink or Hibernate). Most of the time I use JPA as well and use the underlying implementation on the application server. For JBoss default is Hibernate and for Sun it is Oracles Toplink. In this tutorial we use the "old fashioned" Hibernate directly.

Tools used


Eclipse 3.3.1.1 - to edit files
MySql 5.0.5 to store content
Maven 2.0.9 - to build project
Part 2 Apache 2.0 - to run web application
Part 2 Tomcat 5.5.17 - to execute java code
Part 5 JBoss 5.0.0 - to run Java EE app

Tutorial One, create a web service project with XFire

This tutorial will show how you can create an application with XFire from Codehouse. It will not access the database and it will have hardwired properties.Note that today most people use the JAX-WS API directly in the Java EE and do not use aweb service framework at all.

Step 1. Create project
People do this differently, but this is how I do it. Create three folders, call the first one book, add two folders within this one, call them bookjar and bookwar. The war project is not needed for the moment.
I always add a suffix to my projects to indicate which type of packaging to use, jar, war, ejb or ear. In each folder add a file called pom.xml.

 

Step 2. Create pom files in each folder

In the top folder book add this pom file whichjust define some aspects of the project. Note that there is probably as many ways as there are developers out there when it comes to writing pom files.

As you can see I have already added the MySql to the pom because I know I will need it later.

In the bookjar folder add this pom file.
In the bookwar folder add this pom file.

Step 3. Create files needed for the build

Now you are almost done. First, before you can build the application you must have directory structure.

The jar project shall have this structure;

  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources

The war project must have this structure;

  • src/main/java
  • src/main/resources
  • src/test/java
  • src/test/resources
  • src/main/webapp/WEB-INF
  • src/main/webapp/META-INF

 It is very important to understand that Maven will not be able to build the application without a web.xml file in the WEB-INF directory. This is just a simple plain one.

If maven do not find the dependency jars you must install them into your repository. Note. Sometimes it can be a bit confusing or some overhead to get Maven to work. For instance you might need jar files which do not exist in the maven repository. These will have to be installed manually. Do not give up, it is worth the extra job. After that you will notice how much easier it is to run the build process.


What we need to do now is create the "stuff" we will work with. I will not go into the details, instead see the import statement in the file header, these will indicate where these files shall reside. I.e. in which java packages they are expected to be found.


Step 4. Create java files needed in the application

Write eclipse:eclipse and import the projects into Eclipse.

We will create 3 java files.

  • se.aja.xfire.Book.java (java class)
  • se.aja.xfire.BookService.java (java class)
  • se.aja.xfire.BookServiceImpl.java (java interface)

One is our domain object (book), this is what the site is about, books. Since it is so small we use the same object from the database out to our jsp pages.This is one strategy, the other is to convert the objects before displaying them.Two is the service. Put all these objects in the jar project. Thus when you are done in the jar project you shall have these files;

  • src/main/java/se/aja/xfire/Book.java 
  • src/main/java/se/aja/xfire/BookService.java
  • src/main/java/se/aja/xfire/BookServiceImpl.java

Step 5. Create XFire service file and modify web.xml

You will need to add the XFire services.xml file to the META-INF directory. When you are done in the war project it shall look like this.

  • src/main/webapp/WEB-INF/web.xml (standard web app file)
  • src/main/webapp/META-INF/services.xml (XFire configuration file)
Services is the main xfire configuration file. In here you tell the system which service is to be published. In our case the service is called BookService. Quite fitting when it comes to my big interest, books.

Step 6. The service.xml file content

The file might look like this.
<beans xmlns="http://xfire.codehaus.org/config/1.0">
  <service>
    <name>BookService</name>
    <namespace>http://xfire.codehaus.org/BookService</namespace>
    <serviceClass>se.aja.xfire.BookService</serviceClass>
    <implementationClass>se.aja.xfire.BookServiceImpl</implementationClass>
  </service>
</beans>
The service class indicates which service to publish and the implementation class is the concrete class implementing the interface.

One strange thing occurd to me, and is is also written about it on codehaus. I got a file not found error. I followed this instruction and got it to work.
Try "WEB-INF/classes/META-INF/xfire/services.xml" if the system cannot find the file. I.e. add a directory META-INF into the classes directory and add the services.xml there.

Step 7. The domain object (Book.java)

Let's assume the book class just contain an author and a title. It might look like this then. Pls. ignore the obious bad programming regarding null pointers and whatsoever. This is a pojo.

package  se.aja.xfire.Book;

import java.io.Serializable;

public class Book implements Serializable{

    private String author;
    private String title;
    
    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = auhtor;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Step 8. The service definition (BookService.java)

BookService.java - Our service interface, remember S(ervice)OA.
The service just publishes some methods.

public interface BookService extends Serializable {
    public List getBooks();
}

Step 9. The service implementation (BookServiceImpl.java)

 BookServiceImpl.java - Our class implementing the interface.

public class BookServiceImpl implements BookService {
    private List books;
    
    public BookServiceImpl(){
        books = new Arrayist();
        Boook b1 = new Book();
        b1.setAuthor("Jonas");
        b1.setTitle("Blaha");

        Boook b2 = new Book();
        b2.setAuthor("Aspemo");
        b2.setTitle("Blaha again");
        
        books.add(b1);
        books.add(b2);
    }
    
    public List getBooks(){
        return books;
    }
}

Step 10. Configure the web.xml file to handle XFire


You have to add the xfire servlet to your web.xml file. Add the following between the webapp tag in that file.

 <servlet>
    <servlet-name>XFireServlet</servlet-name>
    <display-name>XFire Servlet</display-name>
    <servlet-class>
        org.codehaus.xfire.transport.http.XFireConfigurableServlet
    </servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <<url-pattern>/servlet/XFireServlet/*</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>

Your web.xml file should look like this now.

Step 11. Fix context root for Sun Application Server

Your application will default to the name of the war, i.e. war-1.0.0. If you want to define a context name do like this.

Add sun-web.xml file to the WEB-INF directory and add this code there

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.0 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-0.dtd">
<sun-web-app>
  <context-root>/book</context-root>
</sun-web-app>

if running on a Sun Application Server or add a jboss-web.xml into the

Step 12. Fix context root for JBoss Application Server

Your application will default to the name of the war, i.e. war-1.0.0. If you want to define a context name do like this.

Add jboss-web.xml file to the WEB-INF directory and add this code there;

<jboss-web>

 <context-root>book</context-root>

 </jboss-web>

Step 13. Deploy the application
Run the web application by deploying it to your favourite servlet container.

Type,
http://localhost:8080/book/services/BookService?wsdl and you see your exposed web service.

That is what the default context name should be.If you are unsure and wants to define context.

Your first xfire app is working. Test the methods with SOAP UI. Take a break. Drink a coke or a coffee.

Step 14. What's left

Well most obviously we lack the functionality to access things in the database. The Book file is hardwired with some data. That

 

Last Updated ( Friday, 27 February 2009 06:54 )
 

Your are currently browsing this site with Internet Explorer 6 (IE6).

Your current web browser must be updated to version 7 of Internet Explorer (IE7) to take advantage of all of template's capabilities.

Why should I upgrade to Internet Explorer 7? Microsoft has redesigned Internet Explorer from the ground up, with better security, new capabilities, and a whole new interface. Many changes resulted from the feedback of millions of users who tested prerelease versions of the new browser. The most compelling reason to upgrade is the improved security. The Internet of today is not the Internet of five years ago. There are dangers that simply didn't exist back in 2001, when Internet Explorer 6 was released to the world. Internet Explorer 7 makes surfing the web fundamentally safer by offering greater protection against viruses, spyware, and other online risks.

Get free downloads for Internet Explorer 7, including recommended updates as they become available. To download Internet Explorer 7 in the language of your choice, please visit the Internet Explorer 7 worldwide page.