Sunday, September 8, 2013

RESTFul(JAX-RS)

StudentService.java
---------------------
package com.kundan.wstest;

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

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("student")
public class StudentService {
@GET
@Path("getallasjson")
@Produces(MediaType.APPLICATION_JSON)
public List<Student>getAllStudentAsJSON(){
List<Student>studList=new ArrayList<Student>();
Student s1=new Student(101, "kundan", "me@gmail.com", 888);
studList.add(s1);
Student s2=new Student(102, "prem", "pp@gmail.com", 999);
studList.add(s2);
return studList;

}


@GET
@Path("getall")
@Produces(MediaType.APPLICATION_XML)
public List<Student>getAllStudentAsXML(){
List<Student>studList=new ArrayList<Student>();
Student s1=new Student(101, "kundan", "me@gmail.com", 888);
studList.add(s1);
Student s2=new Student(102, "rajeev", "u@gmail.com", 999);
studList.add(s2);
return studList;

}
@GET
@Path("{sid}/getbyidasjson")
@Produces(MediaType.APPLICATION_JSON)
public Student getAllStudentAsJSON(@PathParam("sid")int sid){
Student s2=new Student(105, "kkk", "u@gmail.com", 5555);
return s2;
}

}


Student.java
---------------------
package com.kundan.wstest;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name="student")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="Student")
public class Student {
private Integer sid;
private String sname;
private String email;
private Integer phone;
public Student(){} 
public Student(Integer sid, String sname, String email, Integer phone) {
// TODO Auto-generated constructor stub
this.sid=sid;
this.sname=sname;
this.email=email;
this.phone=phone;
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}

}

web.xml
------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WebServiceTest</display-name>
  <servlet>
  <servlet-name>REST</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>com.kundan.wstest</param-value>
  
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>REST</servlet-name>
  <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

Add the following jar in WEB-INF->lib
------------------------------------------

/WebServiceTest/WebContent/WEB-INF/lib/activation-1.1.jar
/WebServiceTest/WebContent/WEB-INF/lib/asm-3.1.jar
/WebServiceTest/WebContent/WEB-INF/lib/commons-beanutils-1.7.jar
/WebServiceTest/WebContent/WEB-INF/lib/commons-collections.jar
/WebServiceTest/WebContent/WEB-INF/lib/commons-lang.jar
/WebServiceTest/WebContent/WEB-INF/lib/commons-logging-1.1.1.jar
/WebServiceTest/WebContent/WEB-INF/lib/ezmorph.jar
/WebServiceTest/WebContent/WEB-INF/lib/jaxb-api-2.1.jar
/WebServiceTest/WebContent/WEB-INF/lib/jaxb-impl-2.1.10.jar
/WebServiceTest/WebContent/WEB-INF/lib/jersey-core-1.0.3.1.jar
/WebServiceTest/WebContent/WEB-INF/lib/jersey-json-1.0.3.jar
/WebServiceTest/WebContent/WEB-INF/lib/jersey-server-1.0.3.1.jar
/WebServiceTest/WebContent/WEB-INF/lib/json-lib-2.2.2-jdk15.jar
/WebServiceTest/WebContent/WEB-INF/lib/jsr311-api-1.0.jar
/WebServiceTest/WebContent/WEB-INF/lib/stax-api-1.0-2.jar
/WebServiceTest/WebContent/WEB-INF/web.xml


URL:
-------
 http://localhost:8080/WebServiceTest/rest/student/getall
http://localhost:8080/WebServiceTest/rest/student/getallasjson
http://localhost:8080/WebServiceTest/rest/student/105/getbyidasjson

output
---------
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<students>
-<student>
<sid>101</sid>
<sname>kundan</sname>
<email>me@gmail.com</email>
<phone>888</phone>
</student>
-<student>
<sid>102</sid>
<sname>rajeev</sname>
<email>u@gmail.com</email>
<phone>999</phone>
</student>
</students>

No comments:

Post a Comment