Home  >  Web Services  >  RESTEasy 3

JAX-RS RESTEasy 3 @FormParam Annotation Example

By Arvind Rai, March 10, 2015
In this page we will learn JAX-RS @FormParam annotation with RESTEasy 3. @FormParam binds the http request form elements value with JAX-RS method parameters. @FormParam will accept HTML form data or application/x-www-form-urlencoded media type values. If we have many elements in http request form to post, then we can create an entity in which each attribute will be annotated with @FormParam and JAX-RS method will use this entity by RESTEasy @Form annotation. To test the application, we will create a HTML page to post data. We will also test our web service by standalone java class using client RESTEasy framework.

We can use @FormParam in two ways with JAX-RS methods.

1. Using @FormParam as arguments. Find the code snippet.
@POST
@Consumes("application/x-www-form-urlencoded")
public Response saveEmp(@FormParam("id") String id, @FormParam("name") String name, 
    		                   @FormParam("company") String company) {} 
For more than one form element, we can use multiple @FormParam as arguments in our JAX-RS method.

2. If we have many form elements then we will create an entity in which property will be annotated with @FormParam annotation. Create the entity as below.
EmpForm.java
package com.concretepage;
import javax.ws.rs.FormParam;
public class EmpForm {
	@FormParam("id")
	private String id;
	@FormParam("name")	
	private String name;
	@FormParam("company")	
	private String company;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
} 
And in our service class, JAX-RS method will be defined with RESTEasy @Form annotation as argument.
@POST
@Consumes("application/x-www-form-urlencoded")
public Response saveEmpForm(@Form EmpForm form) {} 

Create Service

Find the service class. In the service class, we are creating two methods. One method is using JAX-RS @FormParam as argument and another method is using RESTEasy @Form as argument.
EmployeeService.java
package com.concretepage;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.annotations.Form;
import org.jboss.resteasy.annotations.providers.jackson.Formatted;
@Path("/manage")
public class EmployeeService {
    @POST
    @Path("/save")
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    @Formatted
    public Response saveEmp(@FormParam("id") String id, @FormParam("name") String name, 
    		                          @FormParam("company") String company) {
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("result", "Data Saved 1");    	
    	map.put("id", id);
    	map.put("name", name);
    	map.put("company", company);
        return Response.ok(map).build();
    }
    @POST
    @Path("/saveform")
    @Consumes("application/x-www-form-urlencoded")
    @Produces("application/json")
    @Formatted
    public Response saveEmpForm(@Form EmpForm form) {
    	Map<String,String> map = new HashMap<String,String>();
    	map.put("result", "Data Saved 2");
    	map.put("id", form.getId());
    	map.put("name", form.getName());
    	map.put("company", form.getCompany());
        return Response.ok(map).build();
    }
} 

Create Client

To test the web service, we will use one HTML form page and a java class using RESTEasy client framework.

Test Web service with HTML file

Find the JSP file which will return HTML output.
employeeform.jsp
<html>
  <head><title>Employee Form</title></head>
  <body>
     <form method="POST" action="/resteasyservice-1/employee/manage/save">
       Id:  <input type="text" name="id"/><br/>
        Name: <input type="text" name="name"/><br/>
        Company: <input type="text" name="company"/><br/>
        <input type="submit" value="Submit"/>
     </form>
  </body>
 </html>  
Deploy client project downloading from link given in bottom and access the URL http://localhost:8080/resteasyclient-1/employeeform.jsp
JAX-RS RESTEasy 3 @FormParam Annotation Example
After submit, we will get response as below.
JAX-RS RESTEasy 3 @FormParam Annotation Example

Test Web service with Java Class

To test our code by java class, we need to use JAX-RS client javax.ws.rs.core.Form class.
RESTEasyClient.java
package com.concretepage;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
public class RESTEasyClient {
   public static void main(String[] args) {
	ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target("http://localhost:8080/resteasyservice-1/employee/manage/saveform");
        Form form = new Form();
        form.param("id", "122").param("name", "Atul").param("company", "BBB");
        Entity<Form> entity = Entity.form(form);
        Response response = target.request(MediaType.APPLICATION_JSON).post(entity);
        String value = response.readEntity(String.class);
        System.out.println(value);
        response.close();  
   }
} 
Find the output.
{
  "result" : "Data Saved 2",
  "name" : "Atul",
  "company" : "BBB",
  "id" : "122"
} 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us