title: Building Restful Web Service using Spark Java for Library Management System
body: <p>I am new with Spark Java. I have made a Library Management System with get and post methods written in Java. I have made an HTML form that takes information of user and creates users which gets stored in a <code>ArrayList</code> of objects in Java. When I call get method to return the list of user objects created via ajax, nothing gets returned. Success nor error gives any output.</p>
  
  <p>But when I simply put the url : <a href="http://localhost:4567/users" rel="nofollow">http://localhost:4567/users</a>
  I get the following output: </p>
  
  <pre><code>[{"id":1,"firstName":"Bhavya","lastName":"Chauhan","age":"22","gender":"F","phone":"1234567890"},
  {"id":2,"firstName":"Rashi","lastName":"Chauhan","age":"20","gender":"F","phone":"1233455677"}]
  </code></pre>
  
  <p>I have attached my html and Spark java code. Thanks in advance!</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Text Input Control&lt;/title&gt;
  &lt;script src="https://code.jquery.com/jquery-1.10.2.js"&gt;&lt;/script&gt;
  &lt;meta charset="utf-8"/&gt;
  &lt;/head&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt;    &lt;script type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;script&gt;
  
      $(function () {
  
          $("#userGet").click(function(){
              alert("Here");
              $.ajax({
                  type: 'GET',
                  url: 'http://localhost:4567/users',
                  crossDomain: true,
                  data: '',
                  dataType: 'jsonp',
                  async:false,
                  success: function(responseData) {
                      //$("#divid").html(+responseData); 
                      alert("hi");
                  },
                  error: function(jqXHR, exception) {
                      if (jqXHR.status === 0) {
                          alert('Not connect.\n Verify Network.');
                      } else if (jqXHR.status == 404) {
                          alert('Requested page not found. [404]');
                      } else if (jqXHR.status == 500) {
                          alert('Internal Server Error [500].');
                      } else if (exception === 'parsererror') {
                          alert('Requested JSON parse failed.');
                      } else if (exception === 'timeout') {
                          alert('Time out error.');
                      } else if (exception === 'abort') {
                          alert('Ajax request aborted.');
                      } else {
                          alert('Uncaught Error.\n' + jqXHR.responseText);
                      }
                  }
  
          });
  
      });
  
  });
  
  &lt;/script&gt;
  
  &lt;body&gt;
  
  &lt;h2&gt;Library Management System &lt;/h2&gt;
  &lt;button&gt;Get Stuff&lt;/button&gt;
  &lt;form&gt;
      &lt;h3&gt;Manage Users &lt;/h3&gt;
      &lt;input type=button onClick="location='CreateUser.html'" value='Create User'&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type=submit id="userGet" value='Get All Users'&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type=button onClick="location='UpdateUser.html'" value='Update User'&gt;
      &lt;br&gt;&lt;br&gt;
  &lt;/form&gt;
  &lt;form action="http://localhost:4567/books" method="GET"&gt;
      &lt;h3&gt;Manage Books &lt;/h3&gt;
      &lt;input type=submit value='Get All Books'&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type=button onClick="location='FindBookByName.html'" value='Find Book By Name'&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type=button onClick="location='AddBook.html'" value='Add Book'&gt;
      &lt;br&gt;&lt;br&gt;
      &lt;input type=button onClick="location='CheckOutBook.html'" value='Check Out Book'&gt;
      &lt;br&gt;&lt;br&gt;
  &lt;/form&gt;
  
  &lt;div id="divid"&gt;&lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is the Java:</p>
  
  <pre><code>import static spark.Spark.*;
  public class UserController extends JsonUtil
  {
      public UserController(final UserService userService)
      {
          System.out.println("in user controller");
          get("/users", (req, res) -&gt; userService.getAllUsers(), json());
          get("/books", (req, res) -&gt; userService.getAllBooks(), json());
          post("/users", (req, res) -&gt; userService.createUser(req.body()), json());
          post("/users/:id", (req, res) -&gt; userService.updateUser(req.body()),json());
          post("/books", (req, res) -&gt; userService.addBook(req.body()), json());
          post("/books/:name", (req, res) -&gt;     userService.findBookByName(req.body()),json());
          post("/checkedOut", (req, res) -&gt; userService.checkOutBook(req.body()), json());
      }
  }
  </code></pre>