CXF REST Without Spring

CXF REST Without Spring explains step by step details of Creating / Developing CXF JAX-RS (Restful) services without any spring dependencies

JAX-RS is Java API for RESTful Webservices which is very rely upon Representational State Transfer model, you can view JAX-RS specification. JAX-RS uses annotations for simplifying the development efforts.

Most of the tutorial related to Apache CXF is developed using spring framework, this is because of easy integration with spring. But it is not a necessity.

You can see the below example, which is demonstrating How to create a Restful service using CXF without any spring dependencies

Required Libraries

You need to download

  1. JDK 7
  2. Eclipse 4.2
  3. CXF-2.7.3
  4. Tomcat 7

Following jar must be in classpath

  1. cxf-2.7.3.jar
  2. httpasyncclient-4.0-beta3.jar
  3. httpclient-4.2.1.jar
  4. httpcore-4.2.2.jar
  5. httpcore-nio-4.2.2.jar
  6. neethi-3.0.2.jar
  7. javax.ws.rs-api-2.0-m10.jar
  8. xmlschema-core-2.0.3.jar
  9. jettison-1.3.3.jar (JSON library)

CXF REST Without Spring

I am creating a sample restful service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFRestfulTutorial" according to following screenshot

CXF REST Without Spring CXF REST Without Spring

Create a Student Object

package com.student;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Student")
public class Student {
 
private String name;

 
public String getName() {
   
return name;
 
}

 
public void setName(String name) {
   
this.name = name;
 
}

}
Here @XmlRootElement(name = "Student"), is a JAXB convention specifies that Student is XML document.

If you are specifies that @Produces("application/json") then Jettison library converts the JAXB to json text as response

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 implement 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

web.xml

<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        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_3_0.xsd">
	<servlet>
		<display-name>CXFNonSpringJaxrsServlet</display-name>
		<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
		<servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
		</servlet-class>
		<init-param>
			<param-name>jaxrs.serviceClasses</param-name>
			<param-value>com.student.ChangeStudentDetailsImpl</param-value>
		</init-param>
		<init-param>
			<param-name>jaxrs.address</param-name>
			<param-value>/rest</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFNonSpringJaxrsServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

Publishing CXF Restful Service

Run CXF REST Without Spring

Note

You can Find all the deployed JAX-WS/JAX-RS services you need to append 'services' at the end of the URL so URL will become following

http://localhost:8080/CXFRestfulTutorial/services

Deployed REST Web Service Using CXF

CXF REST Without Spring

Note

you can also see CXF Restful Client in order to run this restful service









2 Responses to "CXF REST Without Spring"
  1. jan 2012-10-11 10:09:53.0
  1. Demerre 2016-07-27 10:10:38.0

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