Login

Create a webservice using cxf2

Rodrigo

By Rodrigo

Created 2014-07-25 22:50:39 Modified 2014-09-17 23:28:52

Apache CXF is a framework to develop webservice which automatically "marshall" the returned objects, so we can just say, ok this method will return an plain object "User" or a "Book" object and the webservice will return a "marshalled" (object as xml) object, or a "marshalled" list of objects

In this tutorial we are going to use this framework to develop a webservice which returns a list of "User" and a single "User" object

this tutorial was written using the following gear:

  • Hardware:           laptop Thinkpad E430 Intel I5, 4gb Ram, 500gb HDD
  • OS:                    x86_64 GNU/Linux Ubuntu 14.10
  • Kernel:               3.13.0-32-generic
  • IDE/Text editor:   Eclipse Java EE IDE for Web Developers Version: Kepler Service Release 2
  • JDK:                   Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
  • Cxf:                    apache-cxf-2.6.13
  • Tomcat:              apache-tomcat-6.0.35 (as a application server)
  • Browser:             Chromium Version 34.0.1847.116 Ubuntu 14.04 aura (260972)

 

1.- Setting up the environment:

First we need to install the cxf enviroment in eclipse, so go to http://cxf.apache.org/download.html extract it wherever you want, now in eclipse and go to windows => preferences => Web Services => CXF 2.x Preferences and click add

then click browse

and choose the path in where you extracted cxf

as you may see when we give the path, automatically recognize the version and type, now click finish


now here click the checkbox and then apply and ok, and that's it we have installed the environment to work with CXF

2.- Creating the web service project:

Once installed the cxf enviroment we can create our project to work on, now go to eclipse file => new => Project... and type dynamic, choose the "Dynamic web project" and click next

here type the project name "webServiceCxf2" and click modify in order to add the CXF facet

here click the CXF2.x checkbox and then ok

here click finish and we will have our project with the CXF facet loaded

 

3.- Creating the classes:

Now we are ready to develop our webservice methods, let's create the bean User in com.rrivero.model package:

package com.rrivero.model;

import java.io.Serializable;

public class User implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private long id;
	private String name;
	private String surname;
	private String address;
	private int age;
	private boolean premiumCustomer;	
	
	public User() {}
	
	public User(long id, String name, String surname, String address, int age,boolean premiumCustomer) {
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.address = address;
		this.age = age;
		this.premiumCustomer = premiumCustomer;
	}
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isPremiumCustomer() {
		return premiumCustomer;
	}
	public void setPremiumCustomer(boolean premiumCustomer) {
		this.premiumCustomer = premiumCustomer;
	}
	
	
}

now create the webservices methods which return a list of User and a single User let's create the "webservice" class in com.rrivero.web package:

package com.rrivero.web;

import java.util.ArrayList;
import java.util.List;

import com.rrivero.model.User;

public class UserMngt {
	
	public User findById(long id){
		
		//here we should have read the user by id going to the database, we are not using databases here so we fill the object without db
		User user =  new User(id, "Rodrigo", "Rivero", "Santiago", 23, true);
		return user;
	}
	
	public List findByName(String name){
		
		//here we should have read the user by id going to the database, we are not using databases here so we fill the object without db
		List userList= new ArrayList();
		User user1 =  new User(1, name, "Rivero", "Santiago", 23, true);
		User user2 =  new User(2, name, "Sáez", "Concepción", 17, false);
		userList.add(user1);
		userList.add(user2);
		return userList;
	}

}

4.- Creating the webservice:

Up to this point we have created the 2 classes; the User bean which is just a bean, and the UserMngt which contains the logic returning a list of User and a single User using 2 differents methods, now let's make the webservice publishing those 2 methods.

Right click on the UserMngt class => Web Services => Create Web Service and select the runtime option

here select Apache CXF 2.x, and as a server Tomcat x

now here click finish and wait; what Eclipse is doing here is creating the webservice, publishing the methods of the selected class (UserMngt) and starting tomcat

if the world is happy up to this point we should have the following list of directories

 

5.- Testing the webservice:

Up to this point we are done, now let's go to http://127.0.0.1:8090/webServiceCxf2/services you'll see the following page:

then to see the wsdl click the link WSDL: and you'll see the following page:

 

testing with soapui; findById method request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://web.rrivero.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:findById>
         <arg0>1</arg0>
      </web:findById>
   </soapenv:Body>
</soapenv:Envelope>

findById method response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:findByIdResponse xmlns:ns2="http://web.rrivero.com/">
         <return>
            <address>Santiago</address>
            <age>23</age>
            <id>1</id>
            <name>Rodrigo</name>
            <premiumCustomer>true</premiumCustomer>
            <surname>Rivero</surname>
         </return>
      </ns2:findByIdResponse>
   </soap:Body>
</soap:Envelope>

testing with soapui; findByName method request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://web.rrivero.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:findByName>
         <!--Optional:-->
         <arg0>Rodrigo</arg0>
      </web:findByName>
   </soapenv:Body>
</soapenv:Envelope>

findByName method response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:findByNameResponse xmlns:ns2="http://web.rrivero.com/">
         <return>
            <address>Santiago</address>
            <age>23</age>
            <id>1</id>
            <name>Rodrigo</name>
            <premiumCustomer>true</premiumCustomer>
            <surname>Rivero</surname>
         </return>
         <return>
            <address>Concepción</address>
            <age>17</age>
            <id>2</id>
            <name>Rodrigo</name>
            <premiumCustomer>false</premiumCustomer>
            <surname>Sáez</surname>
         </return>
      </ns2:findByNameResponse>
   </soap:Body>
</soap:Envelope>

 

 

Download it in https://drive.google.com/file/d/0BwOqSob-RrBOOEk5WUx6RFVQQWs/edit?usp=sharing

6.- Conclusion:

As you may see CXF2.x allows you to return the objects as you wish, without serializing the objects by yourself, I really recommend this framework instead of apache axis, it's pretty simple to use and you don't need to worry about marshalling objects

7.- Reference links:




comments powered by Disqus