RESTFul Servlets

I already wrote the blog on RESTFull Dynamic service proxy ( have a Coffee cup ). In this blog, address the use of Servlets, because Servlets are best candidates for RESTFul services. The main advantage is HTTPServlet which cover the HTTP communication completely.

RESTFul (HTTP method) HTTPServlet
GET doGet()
POST doPost()
PUT doPut()
DELETE doDelete()

In this blog, I just use doPost() to store Person object in the server side Map (employees) and get the complete list of people (employees) as a response. All of the implementation from the RESTFull Dynamic service proxy blog, except the PersonService, which is replaced by the PersonServlet.java as shown in the following listing.

package com.blogspot.ojitha.wsex7.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.blogspot.ojitha.wsex6.person.People;
import com.blogspot.ojitha.wsex6.person.Person;

/**
* Servlet implementation class PersonServlet
*/
@WebServlet("/PersonServlet")
public class PersonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

People<Person> people = new People<Person>();

/**
* @see HttpServlet#HttpServlet()
*/
public PersonServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

try {
// unmarshall
JAXBContext jaxbCtx = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
InputStream in = request.getInputStream();
Person person = (Person) unmarshaller.unmarshal(in);
in.close();
people.employees.put(person.getId(), person);

// marshall
marshallPeople(response);

} catch (ParserConfigurationException | JAXBException e) {
e.printStackTrace();
}

}

private void marshallPeople(HttpServletResponse response)
throws JAXBException, ParserConfigurationException, IOException {
JAXBContext jaxbCtx = JAXBContext.newInstance(People.class);
Marshaller marshaller = jaxbCtx.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
OutputStream out = response.getOutputStream();
marshaller.marshal(people, out);
out.close();
}

/**
* @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
*/
protected void doPut(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
*/
protected void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

You can use Curl tool to test the POST method as shown in the following figure.

image

There was a record with id=2, and I added the id=1 which is in the person.xml. The source code is available to download here .

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python