title: POST returned a response status of 500 Internal Server Error in RESTful client communication
body: <p>Folks,</p>
  
  <p>I am working with the RESTful webservice in java and it causing the 500 error while communicating from the Java application</p>
  
  <p>I referred this tutorial to do the same
  <a href="http://www.bhaveshthaker.com/13/introduction-developing-implementing-restful-web-services-in-java/" rel="nofollow">implementing RESTful Web Services in Java</a></p>
  
  <p>here is the RESTful webservice </p>
  
  <pre><code>@Path("WebTest")
  @Produces("application/json")
  
  public class WebTestResource {
  
      private TreeMap&lt;Integer, BookDemo&gt; bookMap = new TreeMap&lt;Integer, BookDemo&gt;();
  
      public WebTestResource() {
      }
  
  
      @GET
      public List&lt;BookDemo&gt; getBooks() {
          List&lt;BookDemo&gt; books = new ArrayList&lt;BookDemo&gt;();
          books.addAll(bookMap.values());
          return books;
      }
  
      @GET
      @Path("{id}")
      public BookDemo getBook(@PathParam("id") int bookId) {
          return bookMap.get(bookId);
      }
  
      @POST
      @Path("add")
      @Produces("text/plain")
      @Consumes("application/json")
      public String addBook(BookDemo book) {
          int id = bookMap.size();
          try{
  
              book.setId(id);
              bookMap.put(id, book);
  
  
              String strFilePath = "D:\\Java™\\RESTful\\test.txt";
              FileWriter file= null;
              try {
                  file = new FileWriter(strFilePath);
                  file.write(book.getBookName());
              } catch (IOException ex) {
                  Logger.getLogger(WebTestResource.class.getName()).log(Level.SEVERE, null, ex);
              }
              finally{
                  try {
                      file.flush();
                      file.close();
                  } catch (IOException ex) {
                      Logger.getLogger(WebTestResource.class.getName()).log(Level.SEVERE, null, ex);
                  }
  
              }
          }
          catch(Exception ex){
  
          }
  
  
          return "Book \"" + book.getBookName() + "\" added with Id " + id;
      }
  
  }
  </code></pre>
  
  <p>Webservice Client to the java application </p>
  
  <pre><code>public class Restful {
      private WebResource webResource;
      private Client client;
      private static final String BASE_URI = "http://localhost:8050/WebRestfulAppTest/services";
  
      public Restful() {
          com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
          client = Client.create(config);
          webResource = client.resource(BASE_URI).path("WebTest");
      }
  
      public &lt;T&gt; T getBook(Class&lt;T&gt; responseType, String id) throws UniformInterfaceException {
          WebResource resource = webResource;
          resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
          return resource.get(responseType);
      }
  
      public String addBook(Object requestEntity) throws UniformInterfaceException {
          return webResource.path("add").type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(String.class, requestEntity);
      }
  
      public &lt;T&gt; T getBooks(Class&lt;T&gt; responseType) throws UniformInterfaceException {
          WebResource resource = webResource;
          return resource.get(responseType);
      }
  
      public void close() {
          client.destroy();
      }
  
  }
  </code></pre>
  
  <p>POJO</p>
  
  <pre><code>@XmlRootElement(name = "BookDemo")
  public class BookDemo {
  private int id;
  
  private String bookName;
  
  private String bookAuthor;
  
  private String bookISBN;
  
  public int getId() {
  return id;
  }
  
  public void setId(int id) {
  this.id = id;
  }
  
  public String getBookName() {
  return bookName;
  }
  
  public void setBookName(String bookName) {
  this.bookName = bookName;
  }
  
  
  public String getBookAuthor() {
  return bookAuthor;
  }
  
  public void setBookAuthor(String bookAuthor) {
  this.bookAuthor = bookAuthor;
  }
  
  public String getBookISBN() {
  return bookISBN;
  }
  
  public void setBookISBN(String bookISBN) {
  this.bookISBN = bookISBN;
  }
  
  
  }
  </code></pre>
  
  <p>main()</p>
  
  <pre><code>public class GetCallService {
      static Restful objRestful = new Restful();
  
  
      public static void main(String[] args){
          try{
          BookDemo book = new BookDemo();
          book.setBookAuthor("ABC");
          book.setBookName("Introduction to RESTful Web Services Example test application");
          book.setBookISBN("ISBN 10: 0-596-52926-0");
          objRestful.addBook(book);
          BookDemo book1 = new BookDemo();
          book1.setBookAuthor("XYZ");
          book1.setBookName("RESTfull web");
          book1.setBookISBN("ISBN 10: 0-596-52926-0 Prajwal");
          objRestful.addBook(book1);
          }
          catch(Exception ex){
              ErrorLog.errorLog(ex);
          }
      }
  }
  </code></pre>
  
  <p>Exception</p>
  
  <blockquote>
    <p>ex = (com.sun.jersey.api.client.UniformInterfaceException)
    com.sun.jersey.api.client.UniformInterfaceException: POST
    <a href="http://localhost:8050/WebRestfulAppTest/services/WebTest/add" rel="nofollow">http://localhost:8050/WebRestfulAppTest/services/WebTest/add</a> returned
    a response status of 500 Internal Server Error</p>
  </blockquote>