javawaveblogs-20

Monday, November 26, 2012

Restful webservice using CFX and Spring




1. Create a new Maven Web project using Netbeans IDE. (Version used for this post is 7.3 beta3)



2. Run through the wizard to create the Maven Web Application. here i have given the name "CFXSpring" as the project name.


3. In the next screen of the wizard select the server of your choice.  Here i have selected Glassfish server which comes bundled with Netbeans.


4. Then click on finish, your project is ready to use. Here you can notice the following folder structure is created by Maven for you.


5. Now its time to add Spring MVC Framework to our application. So right click on the project node in project window and go to properties menu item. IDE will pop up a properties window for you. In that window go to "Frameworks" categories.  In the right panel of the window you will get a empty text area with an "Add" button to its right. Just Click on the Add button.Another framework window will pop up with the list of frameworks works available to choose from.  Select Spring Web MVC from that window.  See the screen shot below for clarity.



6. Now select the Spring Framework version you like to use with this application.  Here i have selected 2.5.6 to use with my application.  see the figure below.


7. Once the above step is done.  Spring frame work will be added to the project by the IDE, by including the needed dependencies to the POM.XML file. By the way adding the dependencies to the project dependencies folder. see the figure below.


8. Now you can see there is a change in the project folder structure also.  A new folder "WEB-INF" is added to the project. That folder will also contain "applicationContext.xml" which is nothing but the Spring Configuration file where we will be wiring our beans and bean dependencies through property or constrructer injection. also you can see "web.xml" which is nothing but the web applications deployment descriptor.

9. In "web.xml" you can see already "org.springframework.web.servlet.DispatcherServlet" is configured for you by the IDE. so no need for us to do any extra configuration with respect to spring.

10. Now its time to add CFX dependencies to POM.XML.  This we will do manually with out using any of the IDE's wizards.

11. Open the "pom.xml" file which will be located in the projectfiles folder of the project.

12. Go to "dependencies" tag. and add the following dependency to it.

<dependency>             <groupId>org.apache.cxf</groupId>             <artifactId>cxf-rt-frontend-jaxrs</artifactId>             <version>2.6.2</version>         </dependency>
         
13.  Now select "Clean and Build" menu item by right clicking on the project node to build the project. Maven will now download the dependencies and pack it with the project.  You can now see the list of dependencies and the jar's added in the dependencies folder of the project to cross verify this.

14. Now open the "web.xml". Add "org.apache.cxf.transport.servlet.CXFServlet" and its corresponding servlet-mapping URL like below.

15. Now create a class called "HelloService.java" in the package "com.mm.cfxspring.rs".

16. Write a method to say hello as you wish.  See below for an example.


package com.mm.cfxspring.rs;
/**
 *
 * @author Muthu
 */
public class HelloService {
    public String sayHello() {
        return "Hello JavaWave";
    }
}

17. Now add the annotation to the above class to make it as restful web service.  To do so you have to add "@Path("/hello")" just above the class name to identify the resource by external clients. "@GET" above the method name to make the service HTTP GET enabled. and " @Produces("text/plain")" to denote the service what the service has to produce to the client.  For more clarity see the example below.


package com.mm.cfxspring.rs;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
 *
 * @author Muthu
 */
@Path("/hello")
public class HelloService {
    @GET
    @Produces("text/plain")
    public String sayHello() {
        return "Hello JavaWave";
    }
}

18. Once it is done. Open "applicationContext.xml" file and add "xmlns:jaxrs="http://cxf.apache.org/jaxrs"" name space to the name space list already added in the xml. and also add "http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" xsd's to the schema location. see blow for clarity.



19. Now modify the applicationContext.xml by adding the rest service bean and also the reference HelloService bean to it like below.

20. do a clean and build by right clicking the project node once the above steps are done.

21. Now you can see a folder added in project called "RESTful Web Service" and it contains a service by name "HelloService" with path reference as "/hello".

22. To test our service run the project by right clicking the project none and selecting the run menu item.

23. Once you hit the URL "/hello" with the proper port number, host name and context you will get the return string from your web service as expected.

24.  The example shown in this blog will return "Hello JavaWave" as a plain text in the browser. 

No comments:

diggthis