Skip to main content

Restful WADL WebService that serves the XML

In this tutorial, I am showing you how to develop a simple  RESTFUL web application that serves the XML request and response
Jar files required :
  1. cxf-bundle-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. javax.ws.rs-api-2.0-m10.jar
  7. jaxb-impl-1.0.6.jar
  8. jettison-1.3.3.jar
  9. neethi-3.0.2.jar
  10. org-apache-commons-logging.jar
  11. xmlschema-core-2.0.jar

1. Creating a simple Bean class
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;
  }

}

2. Create a Web Service Endpoint Interface

package com.student;


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

3.  Create a Web Service Endpoint Implementation


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/xml")
@Produces("application/xml")
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;
  }
}

4. The Web Application Deployment Descriptor

<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>test</display-name>
        <servlet-name>test</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>test</servlet-name>
       <url-pattern>/test/*</url-pattern>
</servlet-mapping>

</web-app>

5. Run it
 You can access this example in the following URL.
  http://localhost:8080/application/test?_wadl

Comments