CXF Restful Tutorial

Create a Service Interface

This service interface will defines which methods of restful service, to be invoked by the client

package com.student;


public interface ChangeStudentDetails {
 
Student changeName(Student student);
  Student getName
();
}

Implement the Service Interface

Here we implment the service interface created on the previous step

Here we are using one example showing with GET method& another with POST method

GET---> Calling this method will not result any changes to the server

POST---> Calling this method will result changes to the server, This have more secure than GET method

package com.student;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Consumes("application/json")
@Produces("application/json")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {

 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) {
   
student.setName("HELLO " + student.getName());
   
return student;
 
}

 
@GET
  @Path
("/getName")
 
public Student getName() {
   
Student student = new Student();
    student.setName
("Rockey");
   
return student;
 
}
}

Note; On the above ChangeStudentDetailsImpl class, implementing an interface is not necessity, you can create restful services without implementing an interface.

@Consumes annotation specifies, the request is coming from the client

you can specify the Mime type as @Consumes("application/xml"), if the request is in xml format

@Produces annotation specifies, the response is going to the client

you can specify the Mime type as @Produces ("application/xml"), if the response need to be in xml format

Create a cxf.xml

CXF is using Spring internally, Finding classes by spring we need to add service implementation beans are added on "jaxrs:serviceBeans".

<?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:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <jaxrs:server id="base" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="StudentService" />
        </jaxrs:serviceBeans>
    </jaxrs:server>
    <bean id="StudentService" class="com.student.ChangeStudentDetailsImpl" />
</beans>





Previous 1 2 3 Next








27 Responses to "CXF Restful Tutorial"
  1. Yevgeny Vasilyev 2012-01-02 07:26:05.0
  1. admin 2012-01-03 07:26:05.0
  1. pep 2012-01-04 07:26:05.0
  1. admin 2012-01-05 07:26:05.0
  1. Sony 2012-01-06 07:26:05.0
  1. Julien 2012-01-07 07:26:05.0
  1. admin 2012-01-08 07:26:05.0
  1. Ruchika 2012-01-09 07:26:05.0
  1. admin 2012-01-11 07:26:05.0
  1. steve 2012-01-12 07:26:05.0
  1. steve 2012-01-13 07:26:05.0
  1. admin 2012-01-14 07:26:05.0
  1. Madusha 2012-01-15 07:26:05.0
  1. admin 2012-01-16 07:26:05.0
  1. Deepak 2012-01-17 07:26:05.0
  1. admin 2012-01-18 07:26:05.0
  1. bibhu 2012-01-19 07:26:05.0
  1. admin 2012-01-20 07:26:05.0
  1. Ram 2012-01-21 07:26:05.0
  1. admin 2012-01-22 07:26:05.0
  1. madhu 2012-01-23 07:26:05.0
  1. admin 2012-01-24 07:26:05.0
  1. madhu 2012-01-25 07:26:05.0
  1. admin 2012-01-26 07:26:05.0
  1. madhu 2012-01-27 07:26:05.0
  1. terry 2012-01-28 07:26:05.0
  1. Shailendra 2012-01-29 07:26:05.0

Your email address will not be published. Required fields are marked *