CXF REST Without Spring

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>




Previous 1 2 3 Next








3 Responses to "CXF REST Without Spring"
  1. jan 2012-10-11 08:09:53.0
  1. Demerre 2016-07-27 08:10:38.0
  1. RAJESH C 2019-01-08 05:19:30.0

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