JAX-RS - @POST with HTML Form Example

[Last Updated: May 30, 2017]

An HTML form by default sends data with content type application/x-www-form-urlencoded.

<form action="api/customers" method="post" >
           Name  <input type="text" name="name" />
         Address <input type="text" name="address" />
    Phone Number <input type="text" name="phone-number" />
                         <input type="submit" value="Submit" />

JAX-RS service method handles such request using @FormParam

public String createCustomer(@FormParam("name") String name,
                             @FormParam("address") String address,
                             @FormParam("phone-number") String phoneNumber) {
        return dataService.addCustomer(name, address, phoneNumber);

We can submit the form request from JAX-RS client as well, instead of an HTML form.

Form form = new Form();
       form.param("name", "Alyssa William")
           .param("address", "1021 Hweitt Street")
           .param("phone-number", "343-343-3433");

Client client = ClientBuilder.newClient();
WebTarget target ="");
Future<String> response = target.

Example Project

Dependencies and Technologies Used:

  • jersey-core-server 2.22.1: Jersey core server implementation.
  • jersey-container-servlet 2.22.1: Jersey core Servlet 3.x implementation.
  • jersey-media-moxy 2.22.1: Jersey JSON entity providers support module based on EclipseLink MOXy.
  • jersey-test-framework-provider-jdk-http 2.22.1: Jersey Test Framework - JDK HTTP container.
  • JDK 1.8
  • Maven 3.0.4

Jxrs Html Form Post Select All Download
  • jaxrs-html-form-post
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • api
          • webapp
        • test
          • java
            • com
              • logicbig
                • example

    To run the application either run the JUnit test CustomerRestServiceTest or run the embedded tomcat server: d:\jaxrs-html-form-post>mvn tomcat7:run -DskipTests and then use the following link to submit the HTML form:


    You will see the response as newly created customer id. You can use the id to access the newly created resource:


    The server will respond back with JSON data.

    Create more customers using the form link.

    To see all customer use the resource link:


    See Also