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" />
</form>
JAX-RS service method handles such request using @FormParam
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
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 = client.target("http://www.example.com/api/customers");
Future<String> response = target.
request(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.TEXT_PLAIN)
.buildPost(Entity.form(form)).submit(String.class);
Example ProjectDependencies 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
|