Frank Appiah Nsiah's Tech Blog

September 3, 2008

Creating a web service using Xfire with Spring in Netbeans 6.1/6.0

Filed under: Spring — frankappiahnsiah @ 12:52 pm
Tags: , ,

This post is a basic introduction to web services using XFire and Spring and demonstrates how easy it is to create web services.In this tutorial you will use Netbeans 6.1/6.0.Netbeans 6.1 has Spring support (It is preferable) and that is an advantage but you still need to download Spring.

To follow the tutorial,you need the following software and resources.

1.JDK version 6 or 5

3. XFire version 1.2.6

4.Spring version 2.5.5

5. MySQL version 5.0

Configuring the IDE and Creating the database

If Netbeans is installed already, skip this section.

1.You have the Netbeans 6.1 download for your platform,double click to install.During installation Choose the customize button and check Apache Tomcat 6.0

2.Start Netbeans after installation. Choose Window >Services (Ctrl + S) and Expand the node Databases.Under “Databases” node you will see MySQL Server,right click and select Properties.Fill in the details:

Server host name: “localhost” (Change if different)

Server port number: “3306″ (Change if different)

Administrator user name: “root” (Change if different)

Administrator password : (default is empty but fill if you changed the password during installation of MySQL.

You should see “MySQL Server at localhost:3306″ now

3.Select “MySQL Server at localhost:3306″ node and Right click > Create database…

Set database name: dbcontact

Expand node “MySQL Server at localhost:3306″ and Right click on the “dbcontact” and click connect

Setting Up the Web Application Project

In this example developing for the Java EE 5 platform, you do not need a full enterprise application because you do not need an EJB module or session beans. Instead, you can create a simple web application and put the entity classes directly in the web application.

Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Web category.

Type ContactService for the project name and set the project location.

-Deselect the Use Dedicated Folder option, if selected.
(This option is available if you are using NetBeans IDE 6.1. For this tutorial there is little reason to copy project libraries to a dedicated folder because you will not need to share libraries with other users or projects.)
Click Next.

-Set the server to Apache Tomcat 6.0 and set the Java EE Version to Java EE 5.

-Set the framework to Spring Web MVC 2.5

Summary
In this exercise you created a Java EE 5 web application with Spring support

Coding the web service

Get the source codes contactdao, icontactdao, contact, contactservice, contactserviceimpl ,contactcontroller and create the classes under the packages with the same name as the codes in the documents.

-Right click on the project and select New > Other >Properties File

set the FileName :jdbc and save the file under WEB-INF

The contents of the file :

# To change this template, choose Tools | Templates
# and open the template in the editor.
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/dbcontact
jdbc.username=root
jdbc.password=frankappiah
hibernate.dialect=org.hibernate.dialect.MySQLDialect
#end of file

-Edit the applicationContext.xml file :

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:util=”http://www.springframework.org/schema/util”
xmlns:p=”http://www.springframework.org/schema/p”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd”>
<import resource=”classpath:org/codehaus/xfire/spring/xfire.xml”/>
<bean id=”service” class=”blog.dao.Contactdao”
p:sessionFactory-ref=”sessionFactory” />

<bean id=”contactService” class=”blog.webservice.ContactServiceImpl”
p:service-ref=”service”/>

</beans>

-Edit the dispatcher-servlet.xml :

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:util=”http://www.springframework.org/schema/util”
xmlns:p=”http://www.springframework.org/schema/p”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd”>

<bean id=”urlMapping” class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”>
<property name=”mappings”>
<props>
<prop key=”/contacts.htm”>contactController </prop>
</props>
</property>
<property name=”order” value=”1″ />
</bean>

<bean id=”viewResolver”
class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
p:suffix=”.jsp”
p:order=”2″
p:viewClass=”org.springframework.web.servlet.view.InternalResourceView”/>

<bean id=”contactController” class=”blog.controller.ContactController”
p:service-ref=”service”/>

<bean id=”annotationHandlerMapping” class=”org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping”>
<property name=”xfire” ref=”xfire” />
<property name=”webAnnotations”>
<bean class=”org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations”/>
</property>
</bean>
</beans>

-Edit the web.xml :

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=”2.5″ xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml
/WEB-INF/contact-data.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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

</web-app>

- Right click on the project and select New > Other > Spring Framework > Spring Configuration File

Set the File Name : contact-data

-Edit the contact-data.xml file :

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:util=”http://www.springframework.org/schema/util”
xmlns:p=”http://www.springframework.org/schema/p”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd”>
<bean id=”propertyConfigurer”
class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
<property name=”location” value=”/WEB-INF/jdbc.properties”/>
</bean>
<bean id=”datasource” class=”org.apache.commons.dbcp.BasicDataSource” destroy-method=”close”
p:url=”${jdbc.url}”
p:username=”${jdbc.username}”
p:password=”${jdbc.password}”
p:driverClassName=”${jdbc.driverClassName}”
p:initialSize=”1″/>

<bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”
p:dataSource-ref=”datasource”>
<property name=”hibernateProperties”>
<props>
<prop key=”hibernate.dialect”>${hibernate.dialect}
</prop>
</props>
</property>
<property name=”annotatedClasses”>
<list>
<value>blog.domain.Contact</value>
</list>
</property>
</bean>
</beans>

- In Netbeans expand the Web Pages and double click on contact.sql

Set connection to “dbcontact” from the drop down

ie :-jdbc:mysql://localhost:3306/dbcontact

- Click on run (next to the drop down)

- Select the Services Tab, then Databases > “dbcontact” connection > Tables > contact and right click > View data…

This shows all data in the table “contact”

- Add the following libraries to the project

*xfire-all-1.2.6.jar

Unzip Spring zip file from spring-framework-2.5.5\lib

add commons-dbcp.jar, hibernate-annotations.jar, hibernate-commons-annotations.jar, hibernate3.jar, jaxws-api.jar, jdom-1.0.jar, jws-api.jar, persistence.jar, wsdl4j-1.5.2.jar, dom4j-1.6.jar, jta.jar, commons-collections.jar, commons-beanutils-1.7.0.jar, commons-langs-2.1.jar, cglib-nodep-2.1_3.jar, commons-pool.jar, antlr-2.7.6.jar- Right click Libraries and Select Add Library> MySQL JDBC Driver

Running the project

1.Right the project and Select Undeploy and Deploy.

2.Open a browser and point the url to

http://localhost:8080/ContactService/services/contactService?wsdl

If you have problem ,then possibly check the port of your Server.It thens to 8084 if you have GlassFish Server also installed.

Output


2 Comments »

  1. Thanks for providing a very good post on Web service using Spring , the only problem is I have not found contact.sql file to download , If you can provide me with this it ,it will be very helpful for me also , I want to integrate Spring Web Services With ActiveMQ ,if you can provide me with the sample code for the same

    Comment by Pranay — March 19, 2009 @ 9:53 am | Reply

    • Hi Pranay
      Sorry for the missing contact.sql file. You can Download the whole project with the contact.sql.

      Visit this link to see how JMS Transport can be wired for Xfire.

      Comment by frankappiahnsiah — March 20, 2009 @ 11:34 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.