Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import com.google.gson.Gson; import com.google.gson.JsonObject; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author bbalt */ @WebServlet(urlPatterns = { "/api/quote/*" }) public class rest extends HttpServlet { private Map<Integer, String> quotes = new HashMap() { { put(1, "Friends are kisses blown to us by angels"); put(2, "Do not take life too seriously. You will never get out of it alive"); put(3, "Behind every great man, is a woman rolling her eyes"); } }; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JsonObject quote = new JsonObject(); int key = 1; //Get the second quote quote.addProperty("quote", quotes.get(key)); String jsonResponse = new Gson().toJson(quote); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override public String getServletInfo() { return "Short description"; }// </editor-fold> }