Bright Java Tutorial

is brought to you by DoITBright LLC

REST Web Service

JAX-RS HTTP POST Example of REST Web Service

JAX-RS is a Java API specification that provides support in creating or developing RESTful web services. In this lesson, we will be creating a simple RESTful web service that is accessible via HTTP POST method and is implemented via JAX-RS. It is assumed that you have went through the JAX-RS HTTP GET Example of REST Web Service before proceeding through this tutorial.

Open in your Eclipse the 'RestJumpStart' Java project and we will need to modify the '' class.



  import javax.servlet.http.HttpServletResponse;

  public class AccountService {

    public String getAccounts(){
      return "<accounts><account><accountId>1</accountId>" 
        + "<firstName>Rolan</firstName>" 
        + "<lastName>Liwanag</lastName>" 
        + "</account></accounts>";
    public String createAccount(String representation) {
      System.out.println("executing UsersResource.createUser()");
        = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"  
        + representation;
      return representation;
    public String updateAccount(
        @FormParam("accountId") int accountId,
        @FormParam("firstName") String firstName,
        @FormParam("lastName") String lastName,
        @Context HttpServletResponse servletResponse) 
        throws IOException {
      System.out.println(">>> accountId = " + accountId);
      System.out.println(">>> firstName = " + firstName);
      System.out.println(">>> lastName = " + lastName);
      return "<result>success</result>";

As you can see, we added a new method named 'updateAccount' which accepts the parameters 'int accountId', 'String firstName', and 'String lastName' and returns a 'java.lang.String' parameter. We made the method to be accessible via HTTP POST method by adding the '@POST' annotation on top of the method declaration. It is also important to know that we added the annotation '@Consumes(MediaType.APPLICATION_FORM_URLENCODED)' to indicate that it is expecting form parameters which are 'accountId', 'firstName' and 'lastName'. We also added the annotation '@Produces(MediaType.APPLICATION_XML)' to indicate that the web service will be returning an XML as a response. There is nothing special on the 'web.xml' file. It is still the exact same copy of what we had in the previous lesson. Just for the sake of convenience, I am placing it below.

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi=""
      id="WebApp_ID" version="3.0">
    <display-name>Rest Jump Start</display-name>
        Jersey RESTful Application

        Jersey RESTful Application

Deploy your application by copying your 'RestJumpStart' directory to your '/tomcat/webapps/' directory. Then, open a command prompt and start your Apache Tomcat application server. You can now invoke your RESTful web service by invoking it in a REST client like Postman or SOAPUI using the HTTP POST method and the URL 'http://localhost:8080/RestJumpStart/rest/AccountService/accounts'. However, it is better for us to learn how to invoke RESTful web services programmatically using Java. Using the Java project named 'RestClientJumpStart' that we created in the previous lesson JAX-RS HTTP PUT Example of REST Web Service, modify the class 'AccountClient' that is inside the package 'com.example.restclient'. Here is the full source code for it.

  package com.example.restclient;


  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.HttpException;
  import org.apache.commons.httpclient.methods.PutMethod;
  import org.apache.commons.httpclient.methods.StringRequestEntity;

  public class AccountClient {

    public static void main(String[] args) {
	  AccountClient ac = new AccountClient();
    public void callPutMethod() {
      HttpClient client = new HttpClient();
      try {
        PutMethod put = new PutMethod("" + 
          + "AccountService/accounts");
        System.out.println("start of POST method");
        put.setRequestEntity(new StringRequestEntity("" 
          + "ryanjaredliwanag" 
          + "","application/xml","UTF-8"));
        int statusCode = client.executeMethod(put);
        System.out.println(new String(put.getResponseBody()));
        System.out.println("end of POST method\n");
      } catch(HttpException httpe) {

      } catch (Exception e) {

    public void callPostMethod() {
      Client client = ClientBuilder.newClient();
      Form form = new Form();
      form.param("accountId", "1");
      form.param("firstName", "Pedro");
      form.param("lastName", "Penduko");

      String callResult ="http://localhost:8080/RestJumpStart" 
        + "/rest/AccountService/accounts")
      System.out.println("*** Call result = " + callResult);

If you have not finished doing this, download the commons-codec-1.3.jar, commons-httpclient-3.1.jar and commons-logging-1.1.1.jar, place it on your /RestClientJumpStart/lib' directory and include the JAR files in your 'Java Build Path'. In addition, copy all your JAR files inside the '/RestJumpStart/WEB-INF/lib' directory and paste it inside your 'RestClientJumpStart/lib' directory. Add all of these JAR files in your 'Java Build Path'.

REST Client Apache Commons And JAX-RS

Run the client application as a 'Java Application'. You are now able to invoke a REST web service application via a Java application using the HTTP POST method.

Back    Next